- Related Questions & Answers
- 8085 program to check whether the given number is even or odd
- Java program to find whether given number is even or odd
- Check whether given floating point number is even or odd in Python
- Python Program to Determine Whether a Given Number is Even or Odd Recursively
- C Program to Check if count of divisors is even or odd?
- How to Check if a Number is Odd or Even using Python?
- C# Program to check if a number is Positive, Negative, Odd, Even, Zero
- Java Program to check if count of divisors is even or odd
- PHP program to check if the total number of divisors of a number is even or odd
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
- Check whether product of 'n' numbers is even or odd in Python
- Check whether the length of given linked list is Even or Odd in Python
- C program to Check Whether a Number is Positive or Negative or Zero?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

A number is even if it is divisible by two and odd if it is not divisible by two.

Some of the even numbers are −

2, 4, 6, 8, 10, 12, 14, 16

Some of the odd numbers are −

1, 3, 5, 7, 9, 11, 13, 15, 17

A program to check whether number is even or odd using modulus is as follows.

#include <iostream> using namespace std; int main() { int num = 25; if(num % 2 == 0) cout<<num<<" is even"; else cout<<num<<" is odd"; return 0; }

25 is odd

In the above program, the number num is divided by 2 and its remainder is observed. If the remainder is 0, then the number is even. If the remainder is 1, then the number is odd.

if(num % 2 == 0) cout<<num<<" is even"; else cout<<num<<" is odd";

A number is odd if it has 1 as its rightmost bit in bitwise representation. It is even if it has 0 as its rightmost bit in bitwise representation. This can be found by using bitwise AND on the number and 1. If the output obtained is 0, then the number is even and if the output obtained is 1, then the number is odd.

A program to check whether number is even or odd using Bitwise AND is as follows −

#include <iostream> using namespace std; int main() { int num = 7; if((num & 1) == 0) cout<<num<<" is even"; else cout<<num<<" is odd"; return 0; }

7 is odd

In the above program, bitwise AND is done on num and 1. If the output is 0, then num is even, otherwise num is odd.

if((num & 1) == 0) cout<<num<<" is even"; else cout<<num<<" is odd";

Advertisements