Counting How Many Even and Odd Numbers Are There Using For Loop


Explanation

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?

  • Step 1: Declare the variables k, no, e, and d. Initialize e and d with 0.
  • Step 2: Initialize k with 1 in the for loop.
  • Step 3: The loop checks whether k <= 10.
  • Step 4: Accept a number from the user.
  • Step 5: Check whether the entered number is even using no % 2 == 0.
  • Step 6: If the number is even, increase e by 1; otherwise, increase d by 1.
  • Step 7: Repeat the process until all 10 numbers are entered.
  • Step 8: Display the total count of even numbers and odd numbers.

Example Program


#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();

}

Output

Enter any number: 5
Enter any number: 10
Enter any number: 15
Enter any number: 20
Enter any number: 25
Enter any number: 30
Enter any number: 35
Enter any number: 40
Enter any number: 45
Enter any number: 50

5 even Numbers are there
5 odd Numbers are there

Practice Exercises

Task 1: Write a C program using the for loop to read 15 numbers from the user and count how many even numbers and odd numbers are entered.

Goal: Learn how to use the for loop with the if-else statement to count the number of even and odd values separately.

💡 Hint: Initialize e and d with 0. Use the loop condition k <= 15. If no % 2 == 0, increase e by 1; otherwise, increase d by 1.
💡 Show Solution

#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();

}

Output
Enter any number: 2
Enter any number: 5
Enter any number: 8
Enter any number: 11
Enter any number: 14
Enter any number: 17
Enter any number: 20
Enter any number: 23
Enter any number: 26
Enter any number: 29
Enter any number: 32
Enter any number: 35
Enter any number: 38
Enter any number: 41
Enter any number: 44

8 evens are there
7 odds are there
Explanation: The program accepts 15 numbers using a 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.