Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Thursday, 10 March 2022

36.Storing elements into an array using a "for" loop

 Storing elements into an array using a "for" loop:

A for loop is used to accept elements from keyboard and stores into the array.

Here the "for" loop increments the value of "i" by 1 for every iteration from 0 to n-1 where "n" is the size of array.

For every iteration the scanf() accepts an element and stores into a[i].

Here elements may be inserted one by one or all at once by separating with spaces. 








 






























Accessing the elements of array using "for" loop:

A for loop is used to access the elements of array one after another.

Here the "for" loop increments the value of "i" by 1 for every iteration from 0 to n-1 where "n" is the size of array.

For every iteration an element may be fetched (accessed) through a[i].
























Specification1:

Accept 5 elements from the keyboard and print them in accepted order.






Program:

#include<stdio .h="">

void main()

{

int i,a[5];


printf("Enter 5 integers:\n");

for(i=0;i<5 22="" 45="" 5="" 76="" 77="" 98="" a="" accept="" accepted="" an="" and="" are:="" array="" d="" elements="" enter="" execution:="" for="" he="" i="" in="" include="" integers:="" into="" n="" order.="" print="" printf="" program:="" scanf="" specification2:="" stdio.h="" the="" them="" vector="">

void main()

{

int n,i,a[50];


printf("How many elements:");

scanf("%d",&amp;n);

printf("Enter %d elements:\n",n);

for(i=0;i<n 0="" 12="" 34="" 45="" 4="" 50="" 56="" 65="" 67="" 6="" 78="" 87="" a="" accept="" accessed="" allocates="" allocation="" an="" and="" are:="" array="" but="" d="" elements:="" elements="" enter="" example="" execution1:="" execution2:="" explained:="" for="" from="" given="" he="" how="" i="" in="" include="" into="" is="" logic:="" many="" memory="" n-1.="" n="" of="" only="" order.="" print="" printf="" program:="" reverse="" scanf="" specifiation2:="" stdio.h="" that="" the="" them="" to="" use="" user.="" we="">

void main()

{

int n,i,a[50];


printf("How many elements?");

scanf("%d",&amp;n);

printf("Enter %d elements:\n",n);

for(i=0;i<n a="" d="" for="" i="n-1;i" in="" lements="" n="" order:="" printf="" reverse="" scanf="">=0;i--)

  printf("%5d",a[i]);


}


Execution:

How many elements? 5

Enter 5 elements:

12 67 54 34 98

Elements in reverse order:

98 34 54 67 12


Specification3:

Accept "n" elements into a vector and print the sum of all the elements.


Logic:










Program:

#include<stdio .h="">

void main()

{

int n,i,a[50],sum;


printf("How many elements?");

scanf("%d",&amp;n);

printf("Enter %d elements:\n",n);

for(i=0;i<n 12="" 265="" 34="" 54="" 5="" 67="" 98="" a="" accept="" all="" an="" and="" array="" d="" elements.="" elements:="" elements="" enter="" even="" execution:="" for="" how="" i="" include="" into="" logic:="" many="" n="" odd="" of="" print="" printf="" program:="" scanf="" specification4:="" stdio.h="" sum="" the="" um="">

void main()

{

int n,i,a[50],esum,osum;


printf("How many elements?");

scanf("%d",&amp;n);

printf("Enter %d elements:\n",n);

for(i=0;i</n></stdio></n></n></5></stdio>

0 comments:

Post a Comment