The for loop can be used to count how many even numbers and odd numbers are entered by the user. In this program, the user enters 10 numbers. Each number is checked using the if-else statement. If the number is even, the even counter is increased by 1. Otherwise, the odd counter is increased by 1.
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 by 1; otherwise, increase d by 1.#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 + 1; } else { d = d + 1; } } printf("\n %d even Numbers are there", e); printf("\n %d odd Numbers are there", d); getch(); }
for loop to read 15 numbers from the user and count how many even numbers and odd numbers are entered.for loop with the if-else statement to count the number of even and odd values separately.e and d with 0. Use the loop condition k <= 15. If no % 2 == 0, increase e by 1; otherwise, increase d by 1.#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 + 1; } else { d = d + 1; } } printf("\n %d evens are there", e); printf("\n %d odds are there", d); getch(); }
for loop. Each number is checked to determine whether it is even or odd. The corresponding counter is increased by one. After all numbers are entered, the program displays the total count of even numbers and odd numbers separately.