C program to find maximum between three numbers using conditional operator

Write a C program to input three numbers from user and find maximum between three numbers using conditional/ternary operator ?:. How to find maximum between three numbers using conditional operator.

Example

Input

Input num1: 10
Input num2: 20
Input num3: 30

Output

Maximum is 30

Required knowledge

Basic C programming, Conditional operator, Logical operators

Learn how to write the same program using if...else statement.

Learn more - Program to find maximum between three numbers using if...else

Program to find maximum between three numbers

/**
 * C program to find maximum between three numbers using conditional operator
 */

#include <stdio.h>

int main()
{
    int num1, num2, num3, max;

    /*
     * Input three numbers from user
     */
    printf("Enter three numbers: ");
    scanf("%d%d%d", &num1, &num2, &num3);

    /*
     * If num1 > num2 and num1 > num3 then
     *     assign num1 to max
     * else if num2 > num3 then
     *     assign num2 to max
     * else
     *     assign num3 to max
     */
    max = (num1 > num2 && num1 > num3) ? num1 :
          (num2 > num3) ? num2 : num3;

    printf("\nMaximum between %d, %d and %d = %d", num1, num2, num3, max);

    return 0;
}

Important note: In the above conditional operator I have used proper separators (spaces and new lines) to add readability to code. Practice a good habit to add more and more separators in your code to make it clear.

Output

Enter three numbers: 10
20
30

Maximum between 10, 20 and 30 = 30

Happy coding 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>