Total of the Even and Odd Numbers Using For Loop


Explanation

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?

  • 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 number is even using no % 2 == 0.
  • Step 6: If the number is even, add it to e; otherwise, add it to d.
  • Step 7: Repeat the process until all 10 numbers are entered.
  • Step 8: Display the total of even numbers and odd numbers separately.

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 + no;

        }

        else
        {

            d = d + no;

        }

    }

    printf("\n Even total: %d", e);

    printf("\n Odd total: %d", 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

Even total: 150
Odd total: 125

Practice Exercises

Task 1: Write a C program using the for loop to read 15 numbers from the user and find the total of even numbers and odd numbers separately.

Goal: Learn how to use the for loop with the if-else statement to calculate the totals of even and odd numbers separately.

💡 Hint: Initialize 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.
💡 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 + no;

        }

        else
        {

            d = d + no;

        }

    }

    printf("\n Even total: %d", e);

    printf("\n Odd total: %d", 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
Enter any number: 55
Enter any number: 60
Enter any number: 65
Enter any number: 70
Enter any number: 75

Even total: 230
Odd total: 320
Explanation: The program accepts 15 numbers using a 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.