Menu Close

C++ Program to Check Number Even or Odd ( 2 Best Ways )

C++ Program to Check Number Even or Odd

In this tutorial, we are going to write a C++ Program to Check Number Even or Odd. Here will use C++ if..else the statement to check a number entered by the user even or odd.

In previous tutorial we have seen how to swap two numbers.

Before going through this article, let’s understand what exactly is even and odd number.

What is Even Number?

Even number is number which is completely divisible by 2. For example 2, 4, 6, 8, 10 are completely divisible by 2.

What is Odd Number?

Odd number is number which is not divisible by 2. For example 3, 5, 7, 9, are not divisible by 2.

Example 1: C++ Program to Check Number Even or Odd using if else

#include<iostream>
using namespace std;

int main()
{
int number;

cout<<"Enter First Number:- ";
cin>>number;

if ( number % 2 == 0)
{
	cout<<number<<" is a Even Number.";
}
else
{
	cout<<number<<" is a Odd Number.";
}
return 0;
}

Output

Enter First Number:- 12
12 is a Even Number.

Enter First Number:- 23
23 is a Odd Number.

Example 2: C++ Program to Check Number Even or Odd using Ternary Operator.

#include<iostream>
using namespace std;

int main()
{

int number;
cout<<"Enter First Number:- ";
cin>>number;

( number % 2 == 0 ) ? cout<<number<<" is a Even Number." : cout<<number<<" is a Odd Number.";

return 0;

}

Output

Enter First Number:- 18
12 is a Even Number.

Enter First Number:- 29
23 is a Odd Number.

Conclusion:

In this tutorial, you have learned how to write C++ Program to Check Number Even or Odd using if..else and ternary operator.
This is the best way to check a number even or odd entered by the user. I hope this tutorial will help you.id you like this article, please share and comment with your friends who want to write this type of code.

For More Information:- Click Here

Previous Article
Next Article

Related Posts

Leave a Reply