The while 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 number is added to the total variable. After all numbers are entered, the final total of all numbers is displayed.
How It Works?
n with 1 and total with 0.while loop checks whether n <= 10.total variable.n by 1.#include <stdio.h> #include <conio.h> main() { int n = 1, total = 0, no; clrscr(); while(n <= 10) { printf( "\n Enter the Number : " ); scanf( "%d", &no); total = total + no; n = n + 1; } printf( "\n Total = %d", total); getch(); }
while loop to read 15 numbers from the user and find their total.while loop to accept multiple inputs and perform addition operations to calculate the final total.n with 1 and total with 0. Use the while loop condition n <= 15. Accept a number from the user, add it to total, and increase the value of n by 1 after every iteration.#include <stdio.h> #include <conio.h> main() { int n = 1, total = 0, no; clrscr(); while(n <= 15) { printf( "\n Enter the Number : " ); scanf( "%d", &no); total = total + no; n = n + 1; } printf( "\n Total = %d", total); getch(); }
while loop. Each entered number is added to the total variable. The value of n increases by 1 after every input. When all 15 numbers are entered, the final total is displayed.