An array can be used to store multiple values of the same data type. In this program, the user enters 5 numbers into an integer array. A second for loop is then used to calculate the sum of all array elements. Finally, the program displays the total of the entered numbers.
How It Works?
r[5] and the variables k and total.total with 0.for loop to accept five numbers from the user and store them in the array.for loop to access each array element.total.#include <stdio.h> #include <conio.h> main() { int r[5], k, total = 0; clrscr(); for(k = 0; k <= 4; k++) { printf("Enter the Number : "); scanf("%d", &r[k]); } for(k = 0; k <= 4; k++) { total = total + r[k]; } printf("\nTotal = %d", total); getch(); }
for loop.for loop to calculate the total of all array elements.6 and a variable total initialized to 0. Use the first for loop to accept the numbers and the second for loop to add each array element to total.#include <stdio.h> #include <conio.h> main() { int r[6], k, total = 0; clrscr(); for(k = 0; k <= 5; k++) { printf("Enter the Number : "); scanf("%d", &r[k]); } for(k = 0; k <= 5; k++) { total = total + r[k]; } printf("\nTotal = %d", total); getch(); }
6. The first for loop accepts six numbers from the user and stores them in the array. The second for loop accesses each element and adds it to the variable total. After all elements have been processed, the program displays the sum of the array elements.