Q:

C# program to read two numbers and find maximum, minimum number

0

C# program to read two numbers and find maximum, minimum number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C# code to find minimum & minimum number

Here, we are asking for two integer numbers, finding the maximum and minimum and maximum number using if-else statement and the ternary operator.

// C# program to read two numbers 
// and find maximum, minimum number
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            int a;
            int b;
            int min;
            int max;

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

            //finding max number using if-else
            if (a > b)
                max = a;
            else
                max = b;

            //finding min number using if else
            if (a < b)
                min = a;
            else
                min = b;

            //printing
            Console.WriteLine("Using if-else...");
            Console.WriteLine("Minimum number = {0}", min);
            Console.WriteLine("Maximum number = {0}", max);

            //finding max number using ternary operator
            max = (a > b) ? a : b;

            //finding min number using ternary operator
            min = (a < b) ? a : b;

            //printing
            Console.WriteLine("Using Ternary operator...");
            Console.WriteLine("Minimum number = {0}", min);
            Console.WriteLine("Maximum number = {0}", max);

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

Output

Enter first number : 100
Enter second number: 200
Using if-else...
Minimum number = 100
Maximum number = 200
Using Ternary operator...
Minimum number = 100
Maximum number = 200

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# program to find largest of two numbers... >>
<< C# program to demonstrate example of nested switch...