Blogger templates

Pages

Saturday, 21 April 2012

C Program: Find out the Largest Number by using Ternary Operator:

ternary operator
Enter the values of two numbers and find out largest number using conditional
operator(Ternary operator):  

#include<stdio.h>         //  Header File  
#include<conio.h>                                 
void main()                  // Main function of every C program
{
int a,b,c;


 clrscr();
printf("Enter the values of a and b:");               // Output function display the message on screen
scanf("%d%d" ,&a,&b);                      // Input function used to enter or input values
c = a>b ? a : b;                                                   // Ternary operator
printf("Larger number=%d" ,c);
getch();
}
// End of main()


Output1:
Enter the values of a and b:
30
90
Largest number=90

// condition a>b is not TRUE so the value of "b" is executed.

Output2:
Enter the values of a and b:
60
20
Largest number=60

// condition a>b is TRUE so the value of "a" is executed.



0 comments:

Post a Comment