The for loop can be used to read multiple numbers from the user and calculate their total. In this program, the user enters 10 numbers one by one. Each entered number is added to the variable s. After the loop finishes, the program displays the total of all the entered numbers.
How It Works?
k, no, and initialize s with 0.k with 1 in the for loop.k <= 10.s.k by 1.#include <stdio.h> #include <conio.h> main() { int k, no, s = 0; clrscr(); for(k = 1; k <= 10; k = k + 1) { printf("\n Enter any number: "); scanf("%d", &no); s = s + no; } printf("\n Total: %d", s); getch(); }
for loop to read 15 numbers from the user and find their total.for loop to accept multiple inputs and calculate the total of all entered numbers.s with 0. Use the for loop with the condition k <= 15. Read a number during each iteration, add it to s, and display the final total after the loop ends.#include <stdio.h> #include <conio.h> main() { int k, no, s = 0; clrscr(); for(k = 1; k <= 15; k = k + 1) { printf("\n Enter any number: "); scanf("%d", &no); s = s + no; } printf("\n Total: %d", s); getch(); }
for loop to read 15 numbers from the user. Each entered number is added to the variable s. After the loop completes, the program displays the total of all entered numbers.