Even odd program

Even odd program is one of the most common and well known program. A number is called even number if it is divisible by 2. Otherwise the number is odd. In this guide we will see some program to check whether a number is even or odd.

We will use if else statement of C++ to check the reminder after dividing the number by 2. If the reminder is zero then we will print that, the number is even, otherwise odd. However we already have an article to check whether a number is even or odd in C language. You can see that also.

Even odd program using if else condition

In this bellow program the condition inside if block is that the given number is divisible by 2. When the condition returns true, then the program will print the number is even. Otherwise the number will be an odd number.

// even odd program using if else condition in c++

#include<iostream>
using namespace std;

int main(){
  int given_num;

  cout <<"Enter any integer to check here : ";
  cin >>given_num;

  if(given_num % 2 == 0){
    cout <<endl << given_num <<" is an even number." <<endl;
  }else{
    cout <<endl <<given_num <<" is an odd number." <<endl;
  }

  return 0;

}

Output of even odd program

even-odd-program-in-c

Even odd program using ternary operator

In bellow program we have used ternary operator to check whether a number is even or odd in C++. Obviously you can use  the same logic for other languages also.

// even or odd program using ternary operator in c++

#include<iostream>
using namespace std;

int main(){
    int main_number;

    cout <<"Enter the integer to check : ";
    cin >>main_number;

    // using ternary operator to check
    (main_number%2 == 0)? cout << "\nEven.\n" :  cout << "\nOdd.\n";

    return 0;
}

Output of this program:

Enter the integer to check : 345

Odd.
Enter the integer to check : 244

Even.

Previous pageSize of variables

Next pageVowel or consonant