This article explains a simple C# program to determine the greatest number among three user inputs. It demonstrates how to use if-else
conditions and logical operators to compare multiple values. This type of program is often used as an introductory example to teach decision-making in programming.
Code Example
Below is the complete C# code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | static void Main(string[] args) { int number1, number2, number3; string result; Console.Write("Input the first number :"); number1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the second number :"); number2 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the third number :"); number3 = Convert.ToInt32(Console.ReadLine()); if (number1 > number2 && number1 > number3) { result= "The 1st Number is the greatest among three. \n"; } else if (number2 > number1 && number2 > number3) { result = "The 2nd Number is the greatest among three \n"; } else { result= "The 3rd Number is the greatest among three \n"; } Console.WriteLine(result); Console.ReadLine(); } |
Explanation of the Code
- User Input:
- The program prompts the user to input three integers
1 2 3 4 | Console.Write("Input the first number: "); number1 = Convert.ToInt32(Console.ReadLine()); |
Console.ReadLine()
takes the input as a string, andConvert.ToInt32()
converts it into an integer.
Comparison Logic:
- The
if-else
statements compare the three numbers:- First Condition:
if (number1 > number2 && number1 > number3)
- Checks if
number1
is greater than bothnumber2
andnumber3
.
- Checks if
- Second Condition:
else if (number2 > number1 && number2 > number3)
- Checks if
number2
is greater than bothnumber1
andnumber3
.
- Checks if
- Else Condition:
- If neither of the above conditions is true, it assumes
number3
is the greatest.
- If neither of the above conditions is true, it assumes
- First Condition:
1 2 3 | result = "The 1st Number is the greatest among the three.\n"; |
The result is then displayed using.
1 2 3 | Console.WriteLine(result); |
- Program End:
Console.ReadLine()
keeps the console window open until the user presses Enter, allowing them to view the result.
Sample Input and Output
Output:
- Based on the conditions, the program assigns the appropriate message to the
result
variable:
1 2 3 4 5 | Input the first number: 12 Input the second number: 45 Input the third number: 23 |
 Output:
1 2 3 | The 3rd Number is the greatest among the three. |
Solution 2:Â we can use Math.max method for alternative solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | static void Main(string[] args) { int number1, number2, number3,result; Console.Write("Input the first number :"); number1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the second number :"); number2 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the third number :"); number3 = Convert.ToInt32(Console.ReadLine()); result= Math.Max(Math.Max(number1, number2), number3); Console.WriteLine("The biggest number is {0}", result); Console.ReadLine(); } |
Solution 3: Solution with array.max method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | static void Main(string[] args) { int number1, number2, number3; Console.Write("Input the first number :"); number1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the second number :"); number2 = Convert.ToInt32(Console.ReadLine()); Console.Write("Input the third number :"); number3 = Convert.ToInt32(Console.ReadLine()); int[] numbers = { number1, number2, number3 }; int result = numbers.Max(); Console.WriteLine("The biggest number is {0}", result); Console.ReadLine(); } |