Check Even Odd Number Using Conditional Operator in CPP
- Home
- Tutorials
- CPP
- CPP Programs
- Conditional Operator
- 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
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.
- Condition
- ? (Question Mark)
- : (Colon)