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
C
#include <stdio.h>
int main() {
int arr[5];
int i;
printf("Enter 5 integers: ");
for(i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
printf("Displaying integers:\n");
for(i = 0; i < 5; i++) {
printf("%d\n", arr[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.
Note: Note: The loop currently takes 10 inputs but prompts for 5. You can adjust the array size and loop count to 5 for consistency.
Codecrown