C# program to demonstrate example of conditional operator

C# program to demonstrate example of conditional operator

C# example for conditional/ternary operator:

// C# program to demonstrate example of 
// conditional operator
using System;
using System.IO;
using System.Text;

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //finding largest of two numbers 
            int a;
            int b;

            //input numbers 
            Console.Write("Enter first number : ");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter second number: ");
            b = Convert.ToInt32(Console.ReadLine());

            //finding largest number
            int large = (a > b) ? a : b;

            Console.WriteLine("Largest number is {0}", large);


            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output:

Enter first number : 100
Enter second number: 200
Largest number is 200

Recent Posts

error: Alert: Content is protected!!