C# Program for Nested Conditional Operator

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.