Separate and Count The Odd and Even Integers in a entered Integer

09:08 0 Comments A+ a-

Problem:
Write a program that take an integer number from user and print whether every digit of that
number is odd or even. 

For example if user enter the number 5784696 then output should be as:
Odd numbers: 5 7 9 
Even numbers: 8466 
Total odd number: 3 
Total even number: 4 

Note: use increment operator to count the total odd and total even number. 
Hint: You can pull apart a number into its digits using / 10 and % 10

Solution:
# include <iostream>
using namespace std;
int main()
{
int num;
int num1;
int Odd=0;
int Even=0;
int r1=0;
int r2=0;
int x,y;
cout<<"Enter the Number"<<endl;
cin>>num;
while(num!=0){
num1=num%10;
if(num1%2==0){
cout<<num1<<"is even"<<endl;
Even++;
r1=(r1*10)+num1;
}
else{
cout<<num1<<"is odd"<<endl;
Odd++;
r2=(r2*10)+num1;
}
num=num/10;
}


cout<<"The Number of Even Digits are:"<<Even<<endl;
cout<<"The Number of Odd Digits are:"<<Odd<<endl;
x=r1;
y=r2;
cout<<"The Even Numbers are:";
while(x>=1)
{ r1=x%10;
cout<<r1<<" ";
x=x/10;
}
cout<<endl;
cout<<"The Odd Numbers are:";
while(y>=1)
{ r2=y%10;
cout<<r2<<" ";
y=y/10;
}
cout<<endl;
return 0;

}