1
06/11/2024 7:00 pm
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:
1 Answer
1
06/11/2024 7:01 pm
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); } }