Home / Programs / C Program to Read Array Elements
Programming Example

C Program to Read Array Elements

👁 2,172 Views
💻 Practical Program
📘 Step by Step Learning
Learn how to read elements of an array in C programming with this step-by-step tutorial. Understand the basic syntax and usage of the for loop and the scanf() function. See how to use a loop to iterate through the array and read each element. This tutorial is perfect for beginners who are just learning how to work with arrays in C programming, and will give you a solid foundation for more advanced array manipulation tasks.

Information & Algorithm

This is a simple C program that demonstrates how to read elements into an array and print them out.
The program starts by including the stdio.h library, which provides functions for input and output, such as printf and scanf.
In the main function, an integer array "arr" of size 50 is defined and an integer variable "num" is also defined.
The program then prompts the user to enter the number of elements they want to add to the array using the printf and scanf functions.
Then, using a for loop, the program iterates through the array and prompts the user to enter the values of each element using the scanf function.
Finally, another for loop is used to iterate through the array and print out the values of each element using the printf function. The program then returns 0, indicating a successful execution.

Program Code

/*
 C Program to Read Array Elements
 Author: atnyla Developer
 */

#include<stdio.h>
 
int main() {
   int i, arr[50], num;
 
   printf("\nEnter no of elements :");
   scanf("%d", &num);
 
   //Reading values into Array
   printf("\nEnter the values :");
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }
 
   //Printing of all elements of array
   for (i = 0; i < num; i++) {
      printf("\narr[%d] = %d", i, arr[i]);
   }
 
   return (0);
}

Output

Enter no of elements :5

Enter the values :1
2
3
4
5

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5Press any key to continue . . .

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.