This program give you some idea to use ternary operator in C. We can use this operator in place of IF....ELSE. In this example we use this to find greatest and lowest number among 3 number.
Output:
Source Code:
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, sml, grt, sum;
float avg;
printf("Greatest and Lowest Number using Tarnary Opreateor\n");
printf("\n-----------------------------------------------------\n");
printf("Enter 3 no: ");
scanf("%d %d %d",&a, &b, &c);
sum = a + b + c;
avg = (float)sum/3;
grt = (a > b && b > c )? a : ( b > c && b > a)? b : c;
sml = (a < b && b < c)? a : (b < c && b < a)? b : c;
printf("\nSum = %d \nAverage = %.3f", sum, avg);
printf("\nGreater = %d", grt);
printf("\nSmallest = %d", sml);
getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c, sml, grt, sum;
float avg;
printf("Greatest and Lowest Number using Tarnary Opreateor\n");
printf("\n-----------------------------------------------------\n");
printf("Enter 3 no: ");
scanf("%d %d %d",&a, &b, &c);
sum = a + b + c;
avg = (float)sum/3;
grt = (a > b && b > c )? a : ( b > c && b > a)? b : c;
sml = (a < b && b < c)? a : (b < c && b < a)? b : c;
printf("\nSum = %d \nAverage = %.3f", sum, avg);
printf("\nGreater = %d", grt);
printf("\nSmallest = %d", sml);
getch();
return 0;
}
Output:
No comments:
Post a Comment