C Program to Store and Display Array Elements
This program demonstrates how to take multiple inputs from the user, store them in an array, and then print them using a loop in C.
Concept Overview
An array is a collection of elements of the same data type stored in contiguous memory locations. Using loops, we can efficiently input and output array elements.
Program
// Program to take 10 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main() {
int values[10];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 10; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 10; ++i) {
printf("%d\n", values[i]);
}
return 0;
}Output
Enter 5 integers: 10 20 30 40 50 Displaying integers: 10 20 30 40 50
Explanation
- `int values[10];` – Declares an array capable of holding 10 integers.
- `for(int i = 0; i < 10; ++i)` – Loops 10 times to take input values.
- `scanf()` – Reads integers entered by the user and stores them in the array.
- `printf()` – Displays all stored array elements using another loop.