Reputation: 745
While doing some C# exercises got stuck on this one.
// Finding the biggest of five numbers by using only five if statements.
I couldn't get how to make that, so I looked up online I set this up.
Console.Write("Type number 1:"); double a = double.Parse(Console.ReadLine());
Console.Write("Type number 2:"); double b = double.Parse(Console.ReadLine());
Console.Write("Type number 3:"); double c = double.Parse(Console.ReadLine());
Console.Write("Type number 4:"); double d = double.Parse(Console.ReadLine());
Console.Write("Type number 5:"); double e = double.Parse(Console.ReadLine());
double max;
if (a > b && a > c && a > d && a > e)
{
max = a;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
else if (b > a && b > c && b > d && b > e)
{
max = b;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
else if (c > a && c > b && c > d && c > e)
{
max = c;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
else if (d > a && d > b && d > c && d > e)
{
max = d;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
else
{
max = e;
Console.WriteLine("The biggest number from {0} , {1} and {2} is {3}.", a, b, c, max);
}
Using the exercise examples it returns correct data. For example
5 2 2 4 1 return: 5
But after further testing I ended up that it's wrong:
-1 -2 -2 -1 -15 return: -15
Either my first grade math skills are wrong or -15 isn't bigger than -1. I know where the mistake is but I don't know how to actually fix it.
Upvotes: 1
Views: 14573
Reputation: 31
i know it is an old post but i just want to add my own solution to these.
int large = 0;
for (int i = 0; i < 5; i++)
{
Console.Write("Enter interger :");
int num = int.Parse(Console.ReadLine());
if (num > large)
{
large = num;
}
}
Console.WriteLine(large);
Upvotes: 0
Reputation: 186793
First of all, there's a possibility for cheating, i.e.
if (a > b)
max = a;
else
max = b;
is equal to (trenary operator)
max = a > b ? a : b;
of (while loop emulation)
max = b;
while (a > b) {
max = a;
break;
}
Possible honest solution is straightforward and requies 4 if
s only:
...
Double max = a;
if (max < b)
max = b;
if (max < c)
max = c;
if (max < d)
max = d;
if (max < e)
max = e;
Console.WriteLine("The biggest number from {0}, {1}, {2}, {3}, {4} is {5}.", a, b, c, d, e, max);
Upvotes: 1
Reputation: 6888
Quite easy even with only 4 if's.
double max = a;
if (b > max) max = b;
if (c > max) max = c;
if (d > max) max = d;
if (e > max) max = e;
Console.WriteLine("max is " + max);
Upvotes: 6
Reputation: 745
I've found a smarter solution
Console.Write("Type number 1:"); float a = float.Parse(Console.ReadLine());
Console.Write("Type number 2:"); float b = float.Parse(Console.ReadLine());
Console.Write("Type number 3:"); float c = float.Parse(Console.ReadLine());
Console.Write("Type number 4:"); float d = float.Parse(Console.ReadLine());
Console.Write("Type number 5:"); float e = float.Parse(Console.ReadLine());
float max = a;
if (a < b)
{
max = b;
}
if (max < c)
{
max = c;
}
if (max < d)
{
max = d;
}
if (max < e)
{
max = e;
}
Console.WriteLine(max);
works fine.
Upvotes: 0
Reputation: 1611
You should handle equality cases also, else no 'if' statement will match & you end up in the last 'else' block which sets max=e
which is wrong.
Do:
if (a >= b && a >= c && a >= d && a >= e)
{
max = a;
}
else if (b >= a && b >= c && b >= d && b >= e)
{
max = b;
}
else if (c >= a && c >= b && c >= d && c >= e)
{
max = c;
}
else if (d >= a && d >= b && d >= c && d >= e)
{
max = d;
}
else
{
max = e;
}
Console.WriteLine(max);
Upvotes: 1