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.

Also view this program using -
C program to find maximum between two numbers using if else.
C program to find maximum between two numbers using switch case.
C program to find maximum or minimum between two numbers using functions.

Example:
Input first number: 10
Input second number: 20
Output maximum: 20

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;

    /*
     * 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

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment