Header Ads Widget

Ternary Operator

Ternary operator is also known as Conditional operator ( ? and :) .This is written as-

Test Expression ? Expression1 : Expression2

Test Expression is Boolean condition that results either true  or false. firstly Test Expression is evaluated. If the result is true then Expression1 is evaluated and it becomes the value of overall conditional expression. If the result is false then Expression2 is evaluated and it becomes the value of overall conditional expression.

Example : Write a program in C to display entered number is even or odd.

#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Number is even"):printf("Number is odd");
getch();
}

Post a Comment

2 Comments