C# Console If Else Statement

Finding the biggest of three numbers in C#

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:

Explanation of the Code

  1. User Input:
    • The program prompts the user to input three integers
  • Console.ReadLine() takes the input as a string, and Convert.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 both number2 and number3.
    • Second Condition:else if (number2 > number1 && number2 > number3)
      • Checks if number2 is greater than both number1 and number3.
    • Else Condition:
      • If neither of the above conditions is true, it assumes number3 is the greatest.

The result is then displayed using.

  1. 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:

 Output:


Solution 2: we can use Math.max method for alternative solution

 

Solution 3: Solution with array.max method.

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.