The while loop can be used to read multiple numbers from the user and find the total of even and odd numbers separately. In this program, the user enters 10 numbers. Each number is checked using the modulus operator %. If the number is divisible by 2, it is considered an even number and added to the even total. Otherwise, it is added to the odd total.
How It Works?
k with 1, e with 0 for even total, and d with 0 for odd total.
while loop checks whether k <= 10.
no % 2 == 0.
k by 1 and repeat the process.
#include <stdio.h> #include <conio.h> main() { int k = 1, e = 0, d = 0, no; clrscr(); while(k <= 10) { printf( "\n Enter any number: " ); scanf( "%d", &no); if(no % 2 == 0) { e = e + no; } else { d = d + no; } k = k + 1; } printf( "\n Even total: %d", e); printf( "\n Odd total: %d", d); getch(); }
while loop to read 20 numbers from the user and find the total of even numbers and odd numbers separately.while loop with if-else statements to check numbers and calculate even and odd totals separately.k with 1, e with 0 for even total, and d with 0 for odd total. Use the while loop condition k <= 20. Check each number using no % 2 == 0. Add even numbers to e and odd numbers to d. Increase k by 1 after every iteration.#include <stdio.h> #include <conio.h> main() { int k = 1, e = 0, d = 0, no; clrscr(); while(k <= 20) { printf( "\n Enter any number: " ); scanf( "%d", &no); if(no % 2 == 0) { e = e + no; } else { d = d + no; } k = k + 1; } printf( "\n Even total: %d", e); printf( "\n Odd total: %d", d); getch(); }
while loop. Each number is checked using the modulus operator. If the number is divisible by 2, it is added to the even total; otherwise, it is added to the odd total. After all numbers are entered, both totals are displayed separately.