settingsAccountsettings
Menusettings

Q: How to compare two (2) numbers and print the bigger one in C#?

+6 votes

I need to write a program in C# which compares 2 integer numbers and prints as a result (output) the bigger one. (Maybe with if/else statment?)

Please helm me write it!

asked in C# category by user mitko

2 Answers

+3 votes

Try this code:

using System;

class CompareTwoNumbers
{
    static void Main()
    {
        Console.WriteLine("Please enter 2 (two) integer numbers - each on a separate line: ");

        int biggerNumber = int.Parse(Console.ReadLine());
        int smallerNumber = int.Parse(Console.ReadLine());

        if (smallerNumber > biggerNumber)
        {
            biggerNumber = smallerNumber;
        }

        Console.WriteLine("The greater number is: " + biggerNumber);
    }
}
answered by user sam
edited by user golearnweb
+2 votes

another code:

using System;

class CompareNumbers
{
    static void Main()
    {
        Console.WriteLine("Enter 2 numbers: ");
        int firstNumber = int.Parse(Console.ReadLine());
        int secondNumber = int.Parse(Console.ReadLine());

        if (firstNumber > secondNumber)
        {
            Console.WriteLine("The greater number is: {0}", firstNumber);
        }
        else
        {
            Console.WriteLine("The greater number is: " + secondNumber);
        }
    }
}
answered by user samfred5830
edited by user golearnweb
...