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 enter 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 first number: 10
Input second number: 20
Output maximum: 20
Required knowledge
Basic C programmingConditional operator


Program to find maximum using conditional operator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * C program to find maximum between two numbers using conditional operator
 */

#include <stdio.h>

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

    /*
     * Reads two number from user
     */
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    /* Finds maximum using conditional operator */
    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

Comments

Most Viewed

Write C program to enter any year and check whether year is leap year or not using conditional/ternary operator.

WAP to input week number and print week day name.

C Program to Find Third Angle of a Triangle