C program to Read and print the elements of array- Read and Write in Array

 Here, we will learn how to read the elements from the user and then print the elements on the screen using an array.

How to read and print elements in array
Read and Write in Array


An array is a collection of data stored in the memory in continuous manner and one array can hold only one type of data such an array of integers or characters ...etc.

Example : int age[4];  //In this example the array name is age and its size is 4.

Learn more about arrays and its syntax click here.

Index of an array starts from 0. Let us take an example of an array containing 5 elements of integer type.


int arr[5] = {1, 2, 3, 4, 5}

To access the element 1 inside the array we should get the index zero. 

So arr[0] = 1, arr[1] = 2, . . . , arr[4] = 5.

How to read data from the user in arrays:-

To read the elements from the user first we create an array of some data types and then we can use loops to traverse the array elements one by one and then inside the loop we can input the elements from the user one by one and store the element into the array. During each time through the loop we store the elements inside the array at that index position.

Lets see an example of how to read elements in an array using for loop,



	printf("Enter the array elements one by one\n");
    
    for (int i = 0; i < n; i++) {
        printf("Enter %d element in the array: ", i);
        scanf("%d", &arr[i]);
    }
The code snippet that you can see above is to input the elements one by one in an array. In the above example we are asking user to enter the elements inside the array one by one and then inside the for loop we are taking the input from the user each time through the loop.

How to print the elements of an array:-

To print the elements from an array we can use the similar method like how we took input from the user and stored the elements inside the array, here instead of taking the input we just print the elements of the array using loops. During each time through the loop we print the element which is stored at the index position.

Lets see the code snippet to print the elements of an array 


	printf("The entered elements in an array are \n");
    
    for (int i = 0; i < n; i++) {
        printf("The %d element in the array is: %d\n", i, arr[i]);
    }
In the above code snippet we are just printing the elements of an array using for loop.

Note:- The for loop runs from index 0  to index 4 as n is 5 and i < 5. So the elements are stored inside the array starting from the index zero.


Algorithm:-

  1. Start.
  2. Take n from the user.
  3. Declare an array of size n.
  4. Run loop for i=0 through n, and take input from the user and store it in the array.
  5. Again run the loop for i=0 through n and print the elements on the screen.
  6. Stop.
C program to read and print the elements of an array using for loop:-



#include <stdio.h>

int main()
{
    int n;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    
    int arr[n];
    
    printf("Enter the array elements one by one\n");
    
    for (int i = 0; i < n; i++) {
        printf("Enter %d element in the array: ", i);
        scanf("%d", &arr[i]);
    }
    
    printf("The entered elements in an array are \n");
    
    for (int i = 0; i < n; i++) {
        printf("The %d element in the array is: %d\n", i, arr[i]);
    }

    return 0;
}

Output:-
Enter the number of elements: 5

Enter the array elements one by one
Enter 0 element in the array: 1
Enter 1 element in the array: 2
Enter 2 element in the array: 3
Enter 3 element in the array: 4
Enter 4 element in the array: 5

The entered elements in an array are 
The 0 element in the array is: 1
The 1 element in the array is: 2
The 2 element in the array is: 3
The 3 element in the array is: 4
The 4 element in the array is: 5


Output:-
Enter the number of elements: 6

Enter the array elements one by one
Enter 0 element in the array: 12
Enter 1 element in the array: 33
Enter 2 element in the array: 45
Enter 3 element in the array: 76
Enter 4 element in the array: 20
Enter 5 element in the array: 1

The entered elements in an array are 
The 0 element in the array is: 12
The 1 element in the array is: 33
The 2 element in the array is: 45
The 3 element in the array is: 76
The 4 element in the array is: 20
The 5 element in the array is: 1

Post a Comment

0 Comments