C program to find maximum between two numbers using conditional operator

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.

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.

Required knowledge

Operators, Data Types in c, Variables in C

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 first number: "); scanf("%d", &num1); printf("Enter second number:"); scanf("%d", &num2); /* * Using if else statement * 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 first number: 10 Enter second number: 20 Maximum between 10 and 20 is 20




Instagram