How to find greatest of two numbers using conditional operator in C Language

Introduction: In previous article i explained how to Find greatest of three numbers using nested if else statement in C Language and How to swap two numbers without using temporary variable in C Language? and Check whether a number is positive or negative using if else in C Language and Check whether a number is even or odd using if else statement in C Language. Now in this article i will explain how to find greatest of two numbers using conditional operator in C Language. Conditional operator is also known as ternary operator and its symbol is ?:
 The syntax for conditional operator is: condition?expression1:expression2;
Condition is evaluated either true or false as a Boolean expression . If condition is true expression1 will be evaluated and become the result else expression2 will be evaluated and become the result.Let's create a program to understand the working of ternary operator.

#include<conio.h>
       #include<stdio.h>
       main()
       {
             int x,y,large;
             printf("Enter first value: ");
             scanf("%d",&x);
             printf("Enter second value: ");
             scanf("%d",&y);
            large=(x>y)?x:y;
            printf("Largest value among two numbers=%d",large);
            getch();
             }
Now run the program using Ctrl+F9

Output:
Enter first value: 56
Enter second value: 90
Largest value among two numbers=90

Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..