C++ | Finding even digits in a number

Join Programming and Development Chat Groups on Uphub.io

Introduction

This c++ tutorial is to find a even digit from a number. The program below finds the even digit after you enter the number. The program cannot be extended. Go enjoy the program. Lets begin……………

Program to find even digits from a number.


#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int no,i,m,d;
//Input the number.
cout<<"Enter the no."<<endl;
cin>>no;
//cout
cout<<"Even digits in number are:-"<<endl;
//while loop
while(no>0)
{
d=no%10;
m=d%2;
if(m==0)
cout<<d<<endl;
no=no/10;
}
//get character
getch();
}

Output

Enter the no.
2456
Even digits in number are:-
6
4
2

How does it work

  1. You enter the number.
  2. “The even digits are:- ” is printed by cout.
  3. In while loop last digit is separated and tested if even.
  4. If even is true, then prints the digit.

Extending it

The program cannot be extended.

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as int (name them as you want.)
  5. Add the cout and cin of number.
  6. Add the cout of “The even digits are:-“
  7. Add the loop.
  8. Test and print the even digit.

At the end

You learnt creating the c++ program of Finding the even digit from the given number. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Sharing options: