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 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<iostream.h>
       main()
       {
             int x,y,large;
             cout<<"Enter first value: ";
             cin>>x;
             cout<<"Enter second value: ";
             cin>>y;
            large=(x>y)?x:y;
            cout<<"Largest value among two numbers= "<<large;
            getch();
             }
Run the program using Ctrl+F9

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

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..