Dremendo Tag Line

Input a number and check whether all digits are even or not using while loop in C++

while Loop - Question 12

In this question, we will see how to input a number and check if all digits in the number are even digit or not in C++ programming using while loop. To know more about while loop click on the while loop lesson.

Q12) Write a program in C++ to input a number and check if all digits in the number are even digit or not using while loop.

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int n,d,td=0,ed=0;

    cout<<"Enter any number ";
    cin>>n;

    while(n>0)
    {
        d=n%10;
        if(d%2==0)
        {
            ed=ed+1;		// counting the even digits
        }
        td=td+1;			// counting the total number of digits
        n=n/10;
    }

    if(td==ed)
    {
        cout<<"All digits are even digit";
    }
    else
    {
        cout<<"All digits are not even digit";
    }
    return 0;
}

Output

Enter any number 6248
All digits are even digit
video-poster