C# Console If Else Statement

Finding the Biggest of Two Numbers in C#

When working with numbers in programming, comparing two values is a fundamental task. In this article, we will explain how to compare two numbers in C# and determine which one is greater using a simple program.

Code Example

Below is a sample C# code that compares two numbers input by the user and displays the greater number:

Explanation of the Code

  1. User Input:
    • The program first prompts the user to enter two numbers.
    • Console.ReadLine() is used to read input from the user as a string.
    • The Convert.ToInt32() method converts the input string into an integer.
  2. Comparison Logic:
    • An if-else statement is used to compare the two numbers.
    • If number1 is greater than number2, the program outputs that number1 is the greater number.
    • Otherwise, it assumes number2 is greater and outputs the corresponding message.
  3. Output:
    • The Console.WriteLine() method is used to display the result to the user.
    • The placeholders {0} and {1} in the string are replaced with the values of number1 and number2 respectively.
  4. Program End:
    • Console.ReadLine() at the end prevents the program from closing immediately, allowing the user to see the result.

Sample Input and Output

Input:

Output:

Leave a Comment

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