Here is the C program to read and print elements of an array using for loop.
Required Knowledge
Reading Array Elements
We can use scanf function to take a number as input from user and store it in integer array at index i as follows.
scanf("%d", &inputArray[i]);We will use a for loop to take N inputs from user and store them in array from location 0 to N-1.
Printing Array Elements
We can use printf function to print an array element at index i as follows.
printf("%d ", inputArray[i]);We will use a for loop to traverse an array from index 0 to N-1 and print the elements at corresponding indexes.
C program to read and print array elements using scanf, printf and for loop
#include <stdio.h> int main(){ int inputArray[500], elementCount, counter; printf("Enter Number of Elements in Array\n"); scanf("%d", &elementCount); printf("Enter %d numbers \n", elementCount); for(counter = 0; counter < elementCount; counter++){ scanf("%d", &inputArray[counter]); } printf("Array Elements\n"); for(counter = 0; counter < elementCount; counter++){ printf("%d ", inputArray[counter]); } return 0; }Output
Enter Number of Elements in Array 5 Enter 5 numbers 8 3 5 1 6 Array Elements 8 3 5 1 6
Related Topics