Finding the Largest of Three Numbers in C#

This C# program prompts the user to input three numbers and determines the largest among them using conditional statements. It compares the three values and outputs the largest one. This exercise introduces basic input handling, comparison operators, and decision-making in C#.

Source Code 💻

using System;

class Program
{
    static void Main()
    {
        // Prompt user to enter three numbers
        Console.Write("Enter the first number: ");
        int num1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the second number: ");
        int num2 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the third number: ");
        int num3 = Convert.ToInt32(Console.ReadLine());

        // Find the largest number using if-else conditions
        int largest;

        if (num1 >= num2 && num1 >= num3)
        {
            largest = num1;
        }
        else if (num2 >= num1 && num2 >= num3)
        {
            largest = num2;
        }
        else
        {
            largest = num3;
        }

        // Output the largest number
        Console.WriteLine($"The largest number is: {largest}");
    }
}

Explanation

  • The program prompts the user to input three numbers.
  • It uses if-else conditions to compare the three numbers and determine the largest one.
    • If the first number is greater than or equal to the other two, it is the largest.
    • If the second number is greater than or equal to the others, it is the largest.
    • Otherwise, the third number is the largest.
  • The largest number is then displayed.

Example

  • Input: 12, 25, 17
  • Output: The largest number is: 25

🔚 end of document 🔚