- Write a C program to find maximum of three numbers using conditional operator.
- How to find largest of three numbers using ternary operator.
Required Knowledge
C printf and scanf functions
Conditional Operator in C
Algorithm to find maximum of three numbers using conditional operator Let A, B and C are three numbers.
- We will first find the largest of A and B. Let it be X.
- Then we will compare X with third number C to get the overall largest number.
C program to find maximum of three numbers using conditional operator
#include <stdio.h> int main() { int a, b, c, max; /* * Take three numbers as input from user */ printf("Enter Three Integers\n"); scanf("%d %d %d", &a, &b, &c); max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); /* Print Maximum Number */ printf("Maximum Number is = %d\n", max); return 0; }
Output
Enter Three Integers 9 1 4 Maximum Number is = 9