C# program to demonstrate the use of a ternary conditional operator
belongs to collection: C# Basic Programs | If else (conditional statement) programs
All Answers
total answers (1)
belongs to collection: C# Basic Programs | If else (conditional statement) programs
total answers (1)
Program:
The source code to demonstrate the use of a ternary conditional operator is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created a Sample class that contains the Main() method. In the Main() method we declared three local variables num1, num2, and large initialized with 0. Then we read the values of num1 and num2.
Using the above statement, we find the largest number from num1 and num2 and assigned to the variable large. Here if condition (num1>num2) is true then the value of num1 is assigned to the variable large otherwise the value of num2 will be assigned in the variable large. Then finally we printed the value of the variable large on the console screen.