read elements or values into array C programming Language

  Read or input values or elements into Array .
Let understand how to read elements or values into the array..
Given diagram is the flow digram that show how array elements is read and stored into the memory(into array variable arr[]).
Let understand the program that we studied above in detail from the given flow diagram.
i. we declare the variable i,n arr[10] as integer variable.
ii. The printf( "\n Enter number of elements to store in array ");
scanf("%d",&n);
prompt the user "Enter number of elements to store in array "
and scanf("%d",&n) store the entered value in variable n. (let say 5)
printf("Enter %d number of elements an array : \n", n);
the statement again prompt the message " Enter 5 number of elements an array :"
iii. Loop begins i is initialized to i=0 .
the condition i<n is tested .
The scanf("%d",&arr[i]) and i++ repeates till i<n.
 for(i=0;i<n;i++)
 {
  scanf("%d", &arr[i]);
 }
The for loop reads the 5 integer number entered and store them in arr variable.


Read and display Array values.

/* C program to read and display n integer number values in an array */
#include <stdio.h>
int main()
{
  int i,n,arr[10];
 printf("\n Enter the number of elements to store in an array\n");
 scanf("%d",&n);
 printf("Enter %d number of elements an array : \n", n);
 for(i=0;i<n;i++)
 {
  scanf("%d", &arr[i]);
 }
  printf("\nThe Elements in Array are:\n ");
 for(i=0;i<n;i++)
 {
  printf(" %d", arr[i]);
 }
  return 0;
}
output:
Enter the number of elements to store in an array
5
Enter 5 values in the array:
10   21 65 89 53
The Elements in Array are:
10   21 65 89 53
Explanation:
i.  The   for(i=0;i<n;i++)
 {
  scanf("%d", &arr[i]);
 }
Loop given above read 5 integer values and stores them in the array variable named arr.
and the Second for loop
ii.  for(i=0;i<n;i++)
 {
  printf(" %d", arr[i]);
 }
Display the 5 integer values on output screen from the array variable named arr.


Previous Topic:-->> Access elements 1D Array in C || Next topic:-->> 2D Array in C.
Other Topics: