Which is the largest of three numbers?
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
else ifIf the previous condition is false then try this condition instead. You can have as many else if blocks as you need.
A selection with multiple conditions requires each condition to be enclosed in parentheses.
The following logical operators can be used in conditions. They all return true or false:
==
Is equal to. E.g. x == y means is x equal to y?
!=
Is not equal to. E.g. x != y means is x different to y?
<=
Is less than or equal to. E.g. x <= y means is x less than or equal to y?
>=
Is greater than or equal to. E.g. x >= y means is x greater than or equal to y?
You can have as many conditions in one statement as you need. Use parentheses () for order of precedence.
The following Boolean operators can be used to join multiple conditions into a single selection statement that returns either true or false:
&&
Logical and. Both conditions must be true to return true.
||
Logical or. One or more of the conditions must be true to return true.
You can have as many conditions in one statement as you need. Use parentheses () for order of precedence.
Questions to think about with this program to check your understanding:
Explain the purpose of && in the first condition and what this line of code is checking.
Both conditions must be true for the code inside the braces underneath the statement to be executed. Both number1 == number2 and number1 == number3 must be equal. This line checks if all the numbers are the same. Note it is not necessary to check number2 against number3.
Explain why double equals == is required in the conditions and not a single equals =.
Double equals ask the question, “are they both equal?” A single equals means make them equal. We want to check the value of number1 against number2 and number3, not set number1 to be the same as number2.
Change the program so that it:
Enter the first number:
3
Enter the second number:
6
Enter the third number:
9
The largest of the three numbers is the third number.
// Largest number program
using System;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
// Check which of the three numbers is the largest
---
static string largest(int number1, int number2, int number3)
{
---
// All three are the same
if ((number1 == number2) && (number1 == number3))
{
---
return "first";
---
}
---
// The first is the largest
else if ((number1 >= number2) && (number1 >= number3))
{
---
return "first";
---
}
---
// The second is the largest
else if ((number2 >= number1) && (number2 >= number3))
{
---
return "second";
---
}
---
// The third must be the largest
else
{
---
return "third";
---
}
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
Console.WriteLine("Enter the first number:");
int number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second number:");
int number2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the third number:");
int number3 = Convert.ToInt32(Console.ReadLine());
---
Console.WriteLine($"The largest of the three numbers is the {largest(number1, number2, number3)} number.");
---
}
---
}