C++ Program to Find Odd or Even using While Loop

- March 13, 2018
Any integer value that cannot be divided exactly by two is an odd number. Similarly, the value that can be divided by two is even. The following C++ program inputs a number from user and find out if entered number is even or odd.

Logic of Program

In this simple program, the following C++ techniques are used:

  1. while loop
  2. break statement
  3. if else structure

Program Source Code

#include<iostream>
using namespace std;
main()
 {
   int num, odd, prime =1;
   std:: cout<<"Enter any number:";
   std:: cin>>num;
   odd = 2;
   while (odd < num)
    {
      if (num%odd == 0)
      {
         prime = 0;
         break;
      }
    odd++;
    }
 if (prime == 1)
   std:: cout<<num<<"is a odd number";
 else
   std:: cout<<num<<"is a even number";
return 0;
}

Output of Program

Enter any number: 5
5 is a odd number