larger = (a>b)? a:b;
The simple logic behind the program is, when a(variable) is greater than b, the value of a is assigned to larger(variable). Otherwise, the value of b is assigned to larger. The program to find the larger of two numbers using ternary operator is shown below:
#include<conio.h>
#include<stdio.h>
void main()
{
int a,b,larger;
clrscr();
printf("Enter a and b:\n");
scanf("%d%d",&a,&b);
/*The larger of a and b is stored in larger*/
larger=(a>b)?a:b;
printf("Larger=%d\n",larger);
getch();
}
Code generate the Following output
The simple logic behind the program is, when a(variable) is greater than b, the value of a is assigned to larger(variable). Otherwise, the value of b is assigned to larger. The program to find the larger of two numbers using ternary operator is shown below:
#include<conio.h>
#include<stdio.h>
void main()
{
int a,b,larger;
clrscr();
printf("Enter a and b:\n");
scanf("%d%d",&a,&b);
/*The larger of a and b is stored in larger*/
larger=(a>b)?a:b;
printf("Larger=%d\n",larger);
getch();
}
Code generate the Following output