Q:

C++ program to check EVEN or ODD

0

C++ program to check EVEN or ODD

The numbers which are divisible by 2 are known as EVEN numbers while the numbers which are not divisible by 2 are known as ODD.

In this program, we will check whether a given number is EVEN or ODD. Here, we are checking EVEN or ODD by using three different methods.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to check EVEN or ODD using if else in C++

#include <iostream>
using namespace std;

int main()
{
	int num;
	cout<<"Enter an integer number: ";
	cin>>num;
	
	if(num%2==0)
		cout<<num<<" is an EVEN number."<<endl;
	else
		cout<<num<<" is an ODD number."<<endl;
	
	return 0;
}

Output

First run:
Enter an integer number: 10 
10 is an EVEN number. 

Second run:
Enter an integer number: 11 
11 is an ODD number.

Program to check EVEN or ODD using Ternary Operator in C++

(num%2==0)?
	(cout<<num<<" is an EVEN number."<<endl):
	(cout<<num<<" is an ODD number."<<endl);

Program to check EVEN or ODD using Bitwise AND Operator in C++

if(num & 0x01)
    cout<<num<<" is an ODD number."<<endl;
else
    cout<<num<<" is an EVEN number."<<endl;

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now