Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write a program that takes three numbers as input and outputs the maximum of those numbers (C#)

struggling with this task. What’s wrong with my code? Thank you in advance

Console.Clear();
int number1 = int.Parse(Console.ReadLine());
int number2 = int.Parse(Console.ReadLine());
int number3 = int.Parse(Console.ReadLine());
max = 0;
if (number1 > number2)
{
max = number1;
}
if (number1 < number2)
{
   max = number2;
}
if (max > number3)
{
Console.WriteLine(max);
}
else
{
    Console.Write(number2);
}
  

‘Just cannot understand what is wrong. Code is not working’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are two problems with your code.

you need to have number3 in your last else.

else
{
    Console.Write(number3);
}

and also you need to declare max as int.

you also have to ensure the input value is convertible to int in your code.

and it would be best if you also handled the corner cases like when two or three numbers are equal.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading