Odd/Even Program in C++

Program Description

Odd/Even determination for a given number. This is a very basic program that shows, how to determine whether the given number is even or odd in C++ language. The program simply prompts the user for a number, and displays whether the given number is odd or even.

The program uses a basic mathematical property, that every even number is completely divisible by 2. If a number is not completely divisible by 2,it is an odd number. Here we've used the modulo operator (%) to find the remainder after dividing the number by 2.
It's a very simple program, but using the modulo(%) trick, is very helpful in various, not so obvious problems.

*   Odd/Even Program in C++
*	code.cheraus.com

using namespace std;
int main()
  int no;
  cout<<"\nEnter the Number : ";
  if(no%2==1)   //if number if completely divisible(ie. remainder is 0 after dividing by 2)
   cout<<"\nThe number is odd\n";
  cout<<"\nThe number is even\n";
  return 0;

Test Case 1:

Enter the Number : 23

The number is odd

Test Case 2:

Enter the Number : 22

The number is even

Test Case 3:

Enter the Number : 2

The number is even

comments powered by Disqus