codescracker
c++

C++ Program to Check Even or Odd



« Previous Program Next Program »

In this article, you will learn and get code on, to check whether the given number is even or odd in C++. The program is created in following two ways:

Check Even or Odd using if-else

To check whether the given number (by user at run-time) is an even or an odd number in C++ programming, you have to ask from user to enter a number first. Now if it is divisible by 2 (without leaving any remainder), then it is an even number. Otherwise it is an odd number.

#include<iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter a Number: ";
    cin>>num;
    if(num%2==0)
        cout<<"\nIt is an Even Number.";
    else
        cout<<"\nIt is an Odd Number.";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program check odd even

Now enter any number to check whether it is an even or an odd number as shown in the following output with user input as 32:

C++ program odd even

Here is another sample run with user input as 33:

check even or odd number c++

With user input 32. Because 32%2 gives 0. So condition num%2==0 or 32%2==0 or 0==0 evaluates to be true, therefore program flow goes inside the if's body and executes a statement that prints a message, It is an Even Number.

And with user input 33. Because 33%2 gives 2. So condition num%2==0 or 33%2==0 or 1==0 evaluates to be false, therefore in this case, program flow goes to else's body and executes a statement available in the body of else, that prints a message, It is an Odd Number.

Check Even/Odd using Ternary Operator

Now let's create the same purpose program using ternary operator.

#include<iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter a Number: ";
    cin>>num;
    (num%2==0)?(cout<<"\nEven Number"):(cout<<"\nOdd Number");
    cout<<endl;
    return 0;
}

Here is its sample run with user input as 5:

check even odd using ternary operator c++

The condition num%2==0 evaluates to be true. Therefore, the first statement (cout<<"\nEven Number") gets executed, otherwise second statement (cout<<"\nOdd Number") gets executed.

In ternary operator statement,
<condition> ? <true_case> : <false_case>;
shows that, if condition evaluates to be true, then true_case gets evaluated/executed, otherwise false_case gets evaluated.

Previous program can also be replaced with:

#include<iostream>
using namespace std;
int main()
{
    int num;
    char ch;
    cout<<"Enter a Number: ";
    cin>>num;
    ch = num%2==0?'e':'o';
    if(ch=='e')
        cout<<"\nEven Number";
    else
        cout<<"\nOdd Number";
    cout<<endl;
    return 0;
}

Again you can see that, if the condition num%2==0 evaluates to be true, then 'e' gets initialized to ch. Otherwise 'o' gets initialized to ch. Now check for the value of ch, that is whether it equals e or o. If it holds the value e, then print it as even number, otherwise print it as odd number. You can also replace previous program with this one:

#include<iostream>
using namespace std;
int main()
{
    int num, val;
    cout<<"Enter a Number: ";
    cin>>num;
    val = (num%2==0)?1:0;
    if(val==1)
        cout<<"\nEven Number";
    else
        cout<<"\nOdd Number";
    cout<<endl;
    return 0;
}

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker