C# Program for Nested Conditional Operator

• Last Updated : 02 Jul, 2020

Inner conditional operator can be used in any block,it shows like (a>b)?((a>c)?a:c):(b>c?b:c). Here we input three numbers and finding the largest number using nested conditional operator.

Syntax:

```(logical_test1) ?
((logical_test2)? True_block : false_block) :
false_block_outer;
```

By the above conditional operator, it checks the condition one by one if it is true then “true_block” executes, else “false_block” executes and if first is false then “false_block_outer” will be executed.

Example:

```Input: Enter first number : 23
Enter second number: 45
Enter third number : 87

// check by using this method
// (a>b)?((a>c)?a:c):(b>c?b:c);
Output: Largest number is 87
```

Example 1:

C#

 `// C# program to illustrate example of ` `// nested conditional operator ` `using` `System; ` `using` `System.IO; ` `using` `System.Text; ` ` `  `namespace` `Geeks { ` `class` `GFG { ` `   `  `    ``// Main Method ` `    ``static` `void` `Main(``string``[] args) ` `    ``{ ` `        ``// to find largest of three  numbers ` `        ``int` `a; ` `        ``int` `b; ` `        ``int` `c; ` ` `  `        ``// to input numbers ` `        ``Console.Write(``"Enter first number : "``); ` `        ``a = Convert.ToInt32(Console.ReadLine()); ` `       `  `        ``Console.Write(``"Enter second number: "``); ` `        ``b = Convert.ToInt32(Console.ReadLine()); ` `       `  `        ``Console.Write(``"Enter third number : "``); ` `        ``c = Convert.ToInt32(Console.ReadLine()); ` ` `  `        ``// to find largest number ` `        ``int` `large = (a > b) ? ((a > c) ? a : c) : (b > c ? b : c); ` ` `  `        ``Console.WriteLine(``"Largest number is {0}"``, large); ` ` `  `        ``// hit ENTER to exit the program ` `        ``Console.ReadLine(); ` `    ``} ` `} ` `}`

Input:

```Enter first number : 23
Enter second number: 45
Enter third number : 87```

Output:

```Enter first number : 23
Enter second number: 45
Enter third number : 87
Largest number is 87
```

Example 2:

C#

 `// C# program to illustrate example of ` `// nested conditional operator ` `using` `System; ` `using` `System.IO; ` ` `  `class` `GFG { ` `   `  `    ``// Main Method ` `    ``static` `void` `Main(``string``[] args) ` `    ``{ ` `        ``// to find largest of three  numbers ` `        ``int` `a; ` `        ``int` `b; ` `        ``int` `c; ` ` `  `        ``// to input numbers ` `        ``Console.Write(``"Enter first number : "``); ` `        ``a = Convert.ToInt32(Console.ReadLine()); ` `       `  `        ``Console.Write(``"Enter second number: "``); ` `        ``b = Convert.ToInt32(Console.ReadLine()); ` `       `  `        ``Console.Write(``"Enter third number : "``); ` `        ``c = Convert.ToInt32(Console.ReadLine()); ` ` `  `        ``// to find largest number ` `        ``int` `large = (a > b) ? ((a > c) ? a : c) : (b > c ? b : c); ` ` `  `        ``Console.WriteLine(``"Largest number is {0}"``, large); ` `    ``} ` `}`

Input:

```Enter first number : 365
Enter second number: 123
Enter third number : 638```

Output:

```Enter first number : 365
Enter second number: 123
Enter third number : 638
Largest number is 638
```

My Personal Notes arrow_drop_up
Related Articles