C program to find maximum between two numbers using conditional operator

Write a C program to input two numbers and find maximum between two numbers using conditional/ternary operator ?:. How to find maximum or minimum between two numbers using conditional operator in C program.

Example

Input

Input first number: 10 Input second number: 20

Output

Maximum: 20 

There are many approaches to find maximum or minimum. In this post I will explain using conditional operator. Apart from this learn other ways to find maximum or minimum.

Learn more –

Required knowledge

Basic C programming, Conditional operator

Program to find maximum using conditional operator

/** * C program to find maximum between two numbers using conditional operator */ #include <stdio.h> int main() { int num1, num2, max; /* * Input two number from user */ printf("Enter two numbers: "); scanf("%d%d", &num1, &num2); /* * If num1 > num2 then * assign num1 to max * else * assign num2 to max */ max = (num1 > num2) ? num1 : num2; printf("Maximum between %d and %d is %d", num1, num2, max); return 0; }

Output

Enter two numbers: 10 20 Maximum between 10 and 20 is 20

Happy coding 😉