[Solved] Get two numbers and print the greater of them (without if statements)

  

1
Topic starter

I need to write a program which gets two numbers from the console and prints the greater of them.

However - I must do it without implementing the if statements.

Examples:

table c# greater number

1 Answer
1

You can use the Math.Max Method (Double, Double) for this purpose.

It returns the larger of two double-precision floating-point numbers:  https://msdn.microsoft.com/en-us/library/7x97k0y4%28v=vs.110%29.aspx

So the code will look like this:

using System;
 
class NumberComparer
{
    static void Main()
    {
        Console.WriteLine("Please write the first number to compare: ");
        double firstNumber = double.Parse(Console.ReadLine());
 
        Console.WriteLine("Please write the second number to compare: ");
        double secondNumber = double.Parse(Console.ReadLine());
 
        double compareNumbers = Math.Max(firstNumber, secondNumber);
        Console.WriteLine("Number {0} is greater",compareNumbers);
    }
}

compare two numbers in c sharp - Visual Basics with Math.Max method

Share: