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 😉

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>