Even Odd Total Using While Loop


Explanation

The while loop can be used to read multiple numbers from the user and find the total of even and odd numbers separately. In this program, the user enters 10 numbers. Each number is checked using the modulus operator %. If the number is divisible by 2, it is considered an even number and added to the even total. Otherwise, it is added to the odd total.

How It Works?

  • Step 1: Initialize k with 1, e with 0 for even total, and d with 0 for odd total.
  • Step 2: The while loop checks whether k <= 10.
  • Step 3: Accept a number from the user.
  • Step 4: Check whether the number is divisible by 2 using no % 2 == 0.
  • Step 5: If the condition is true, add the number to the even total.
  • Step 6: Otherwise, add the number to the odd total.
  • Step 7: Increase the value of k by 1 and repeat the process.
  • Step 8: Display the total of even numbers and odd numbers.

Example Program


#include <stdio.h>
#include <conio.h>

main()

{

    int k = 1, e = 0, d = 0, no;

    clrscr();

    while(k <= 10)

    {

        printf(
        "\n Enter any number: "
        );

        scanf(
        "%d", &no);


        if(no % 2 == 0)

        {

            e = e + no;

        }

        else

        {

            d = d + no;

        }


        k = k + 1;

    }


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


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


    getch();

}

Output

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

Even total: 150
Odd total: 175

Practice Exercises

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

Goal: Learn how to use the while loop with if-else statements to check numbers and calculate even and odd totals separately.

💡 Hint: Initialize k with 1, e with 0 for even total, and d with 0 for odd total. Use the while loop condition k <= 20. Check each number using no % 2 == 0. Add even numbers to e and odd numbers to d. Increase k by 1 after every iteration.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()

{

    int k = 1, e = 0, d = 0, no;

    clrscr();


    while(k <= 20)

    {

        printf(
        "\n Enter any number: "
        );


        scanf(
        "%d", &no);


        if(no % 2 == 0)

        {

            e = e + no;

        }

        else

        {

            d = d + no;

        }


        k = k + 1;

    }


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


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


    getch();

}

Output
Enter any number: 1
Enter any number: 2
Enter any number: 3
Enter any number: 4
Enter any number: 5
Enter any number: 6
Enter any number: 7
Enter any number: 8
Enter any number: 9
Enter any number: 10
Enter any number: 11
Enter any number: 12
Enter any number: 13
Enter any number: 14
Enter any number: 15
Enter any number: 16
Enter any number: 17
Enter any number: 18
Enter any number: 19
Enter any number: 20

Even total: 110
Odd total: 100
Explanation: The program accepts 20 numbers from the user using a while loop. Each number is checked using the modulus operator. If the number is divisible by 2, it is added to the even total; otherwise, it is added to the odd total. After all numbers are entered, both totals are displayed separately.