Check Even Odd Number Using Conditional Operator in CPP

  1. Home
  2. Tutorials
  3. CPP
  4. CPP Programs
  5. Conditional Operator
  6. Program

Source Code

#include <iostream>

using namespace std;

int main(int argc, char** argv) {
 

int n;  // variable declaration for input
    
    // Taking input in variable
    cout<<"Enter Number : ";
    cin>>n;

    ( n % 2 == 0  ) ? cout<<"Even" : cout<<"Odd";

return 0;
}

Output

C++ program to check even or odd numbers using conditional operator

Working

In conditional operator ? is placed after condition (relational expression). First condition is evaluated and then statements after ? are executed if the given condition is true. Otherwise the statements after : operator are executed.

: (colon) operator is written before false block in conditional operator. In conditional operator three parts are defined, so that this is also called ternary starement or ternary operator.  Ternary operator has three parts.

  1. Condition
  2. ? (Question Mark)
  3. : (Colon)
Comments
Login to TRACK of Comments.