What is Odd Number and Even Number?
A number expressed in the decimal numeral system is even or odd according to whether its last digit is even or odd. That is, if the last digit is 1, 3, 5, 7, or 9, then it is odd; otherwise it is even.
Computer programs can give the results within few Nano seconds.
Here is the sample Program to check whether the given number is Odd or Even in C++.
Coding - Using Two Variables
#include<iostream.h>
#include<conio.h>
int main()
{
int n,k;
cout<<"Enter the Number";
cin>>n:
k=n%2;
if(k==0)
cout<<"Even Number";
else
cout<<"Odd Number";
return 0;
}
#include<conio.h>
int main()
{
int n,k;
cout<<"Enter the Number";
cin>>n:
k=n%2;
if(k==0)
cout<<"Even Number";
else
cout<<"Odd Number";
return 0;
}
Output
Enter the Number 51
Odd Number
Odd Number
Coding - Using Only One Variable
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
cout<<"Enter the Number";
cin>>n:
if((n%2)==0)
cout<<"Even Number";
else
cout<<"Odd Number";
}
#include<conio.h>
void main()
{
int n;
cout<<"Enter the Number";
cin>>n:
if((n%2)==0)
cout<<"Even Number";
else
cout<<"Odd Number";
}
Output
Enter the Number 24
Even Number
Even Number
Turbo C++
When the above C++ Program is compiled and executed, it will produce the following output.
No comments:
Post a Comment