The for loop can be used to read multiple numbers from the user and calculate the total of even and odd numbers separately. In this program, the user enters 10 numbers. Each entered number is checked using the if-else statement. If the number is even, it is added to the even total. Otherwise, it is added to the odd total.
How It Works?
k, no, e, and d. Initialize e and d with 0.k with 1 in the for loop.k <= 10.no % 2 == 0.e; otherwise, add it to d.#include <stdio.h> #include <conio.h> main() { int k, no, e = 0, d = 0; clrscr(); for(k = 1; k <= 10; k = k + 1) { printf("\n Enter any number: "); scanf("%d", &no); if(no % 2 == 0) { e = e + no; } else { d = d + no; } } printf("\n Even total: %d", e); printf("\n Odd total: %d", d); getch(); }
for loop to read 15 numbers from the user and find the total of even numbers and odd numbers separately.for loop with the if-else statement to calculate the totals of even and odd numbers separately.e and d with 0. Use the loop condition k <= 15. Check whether the entered number is even using no % 2 == 0. Add even numbers to e and odd numbers to d.#include <stdio.h> #include <conio.h> main() { int k, no, e = 0, d = 0; clrscr(); for(k = 1; k <= 15; k = k + 1) { printf("\n Enter any number: "); scanf("%d", &no); if(no % 2 == 0) { e = e + no; } else { d = d + no; } } printf("\n Even total: %d", e); printf("\n Odd total: %d", d); getch(); }
for loop. Each number is checked using an if-else statement. Even numbers are added to the variable e, while odd numbers are added to the variable d. After all inputs are completed, the totals of even and odd numbers are displayed separately.