C Program to Store and Display Elements of an Array
This program demonstrates how to declare, store, and display the elements of an array in C using loops.
Concept Overview
An array in C is a collection of elements of the same data type stored at contiguous memory locations. Using loops, we can easily assign and display multiple array values efficiently.
Program
C
#include <stdio.h>
int main() {
int arr[15];
int i;
// Assign values from 10 to 24
for(i = 0; i < 15; i++) {
arr[i] = 10 + i;
}
// Display elements
for(i = 0; i < 15; i++) {
printf("Element[%d] = %d\n", i, arr[i]);
}
return 0;
}
Output
Element[0] = 10 Element[1] = 11 Element[2] = 12 Element[3] = 13 Element[4] = 14 Element[5] = 15 Element[6] = 16 Element[7] = 17 Element[8] = 18 Element[9] = 19 Element[10] = 20 Element[11] = 21 Element[12] = 22 Element[13] = 23 Element[14] = 24
Explanation
- An integer array `n` of size 15 is declared.
- Using a `for` loop, each element is assigned a value starting from 10 using the statement `n[i] = i + 10;`.
- Another loop prints all the array elements with their respective indices.
- Arrays help in managing multiple data items efficiently using a single variable name.
Note: You can modify the array size or take user input to store elements dynamically using `scanf()`.
Codecrown