C program to find largest of 5 numbers using ternary operator
Answers
Answer:
#include<stdio.h>
#include<stdlib.h>
#define MAX_LIMIT 10000.
int main()
{
int a,b,c,d,largest;
//Getting the numbers.
printf("Enter the numbers out of which you have to find the greatest\n ");
Finding largest of 5 numbers using ternary operator:
Output: -
Enter 5 numbers:
66
88
99
10
2
Largest of 5 numbers is: 99
Explanation:
The C program to this question is given below: -
//header file
#include <stdio.h>
//main function
int main()
{
//declaring the variables
int a, b, c, d, e;
//prompts the user to enter 5 numbers
printf("Enter 5 numbers:\n");
//storing the 5 integer values
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
//comparing the values by using ternary operator
//comparing two values and storing the larger number in the f variable
int f = a > b ? a : b;
//comparing another 2 variables
//and storing the larger number in g variable
int g = c > d ? c : d;
//comparing the above resultants
//and storing the larger number in i variable
int i = f > g ? f : g;
//comparing the fifth number with the above resultant
//and storing the larger value in max variable
int max = i > e ? i : e;
//print the largest value
printf("Largest of 5 numbers is:%d ",max);
}
Code Description:
- Including the header file.
- Defining the main function.
- Inside the main function, declaring 5 variables to store the 5 integer values.
- Now, comparing the first two values and storing the larger value in a variable.
- Again comparing the third and fourth value and storing the larger value in a variable.
- Now, comparing the above resultant values and storing the larger value in another variable.
- Finally comparing the above resultant value with the fifth value and storing the larger value in the "max" variable which is the largest value.
- Print the largest value that is the value stored in "max" variable.
Learn more:
Ternary operator: -
https://brainly.in/question/11412110