12th Class NCERT Solution Download Click Here!

X




Sunday, 13 March 2016

Write a program to check whether the given number is even or odd (using ? : ternary operator )

#include<iostream>
using namespace std;

int main()
{
 int a;
 cout<<"Enter the Number : ";
 cin>>a;
 (a%2==0)?cout<<"Number is even":cout<<"Number is odd";

 
 return 0;
}


OUTPUT:

SAMPLE RUN # 1
Enter the Number : 56 Number is even
SAMPLE RUN # 2
Enter the Number : 43 Number is odd
HINT:Ternary Operator ( Its like if and else ) syntax is (condition) ? (if_true) : (if_false) So in above program we checked if the remainder is 0 , if yes (using ? ternary operator) then cout Number is even else (using : ternary operator ) cout number is odd.

No comments:

Post a Comment