In: Computer Science
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
The code will be
#include<stdio.h>
int main() {
int a=9,b=10,c=8;//to be compared
int max=a>b?(b>c?a:(a>c?a:c)):(b>c?b:c);
// the statement above checks whether a is greater than b
/* If yes then b>c if yes then a is max else we have to check
max between a and c
If b is greater than a then we have to check max between b and
c
Similar for minimum just check less than instead of greater
than
*/
int min=a<b?(b<c?a:(a<c?a:c)):(b<c?b:c);
int middle=(a!=max && a!=min)?a:((b!=max &&
b!=min)?b:c);
//the above statement checks the value which is neither max and nor
min it will
//be the middle value
printf("min = %d, middle= %d, max=%d\n",min,middle,max);
printf("Sum = %d\n",a+b+c);
printf("Average = %lf",(a+b+c)/3.0);
}
The output is
Do give a thumbs up