The for loop can be used to read multiple numbers from the user and maintain a running total of even and odd numbers separately. In this program, the user enters 10 numbers. After each number is entered, the program checks whether it is even or odd. If it is even, the number is added to the even total and the updated even total is displayed immediately. Otherwise, the number is added to the odd total and the updated odd total is displayed.
How It Works?
n, e, d, and k. Initialize e and d with 0.k with 1 in the for loop.k <= 10.n % 2 == 0.#include <stdio.h> #include <conio.h> main() { int n, e = 0, d = 0, k; clrscr(); for(k = 1; k <= 10; k++) { printf("\n Enter the Number "); scanf("%d", &n); if(n % 2 == 0) { e = e + n; printf("\n Even Number %d", e); } else { d = d + n; printf("\n Odd Number %d", d); } } getch(); }
for loop to read 15 numbers from the user and display the running total of even numbers and odd numbers after every input.for loop with the if-else statement to calculate and display the running total of even and odd numbers separately.e and d with 0. Use the loop condition k <= 15. If n % 2 == 0, add the number to e and display the updated even total. Otherwise, add the number to d and display the updated odd total.#include <stdio.h> #include <conio.h> main() { int n, e = 0, d = 0, k; clrscr(); for(k = 1; k <= 15; k++) { printf("\n Enter the Number "); scanf("%d", &n); if(n % 2 == 0) { e = e + n; printf("\n Even Number %d", e); } else { d = d + n; printf("\n Odd Number %d", d); } } getch(); }
for loop. Each number is checked to determine whether it is even or odd. If the number is even, it is added to the running total of even numbers and the updated total is displayed immediately. Otherwise, it is added to the running total of odd numbers and the updated total is displayed. This process continues until all 15 numbers have been entered.