Python Program to Find the Largest of Three Numbers

When working with numbers in Python, comparing values is a common task. Finding the largest of three numbers is a simple yet essential problem that can be solved using basic conditional statements. In this blog post, we will explore how to write a Python program to find the largest number among three inputs.


Concept of Finding the Largest Number

To determine which of three numbers is the largest, we need to:

  1. Compare the first number with the other two.
  2. Compare the second number with the remaining one if the first condition fails.
  3. Finally, if neither of the first two numbers is the largest, the third one must be the largest.

This problem can be solved using simple if, elif, and else conditions in Python.


Steps to Write the Program

  1. Input the Three Numbers: We’ll prompt the user to enter three numbers.
  2. Conditional Comparisons: Use if-elif-else statements to compare the numbers.
  3. Display the Result: After comparing, print the largest number.

Python Program to Find the Largest of Three Numbers

# Python program to find the largest of three numbers

# Input: Asking the user to enter three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Check which number is the largest
if num1 >= num2 and num1 >= num3:
    print(f"The largest number is {num1}")
elif num2 >= num1 and num2 >= num3:
    print(f"The largest number is {num2}")
else:
    print(f"The largest number is {num3}")

Explanation:

  1. Input: We use float(input()) to ensure that the program can accept both integers and decimal values from the user.
  2. Conditional Comparison:
  • The program first checks if num1 is greater than or equal to both num2 and num3. If this condition is true, num1 is the largest.
  • If the first condition fails, it checks whether num2 is greater than or equal to both num1 and num3.
  • If neither of these conditions holds true, num3 is the largest number.
  1. Output: The program prints the largest number based on the conditions.

Sample Output

Test Case 1: Largest is the First Number

Enter the first number: 10
Enter the second number: 5
Enter the third number: 3
The largest number is 10

Test Case 2: Largest is the Second Number

Enter the first number: 4
Enter the second number: 9
Enter the third number: 7
The largest number is 9

Test Case 3: Largest is the Third Number

Enter the first number: 2
Enter the second number: 3
Enter the third number: 8
The largest number is 8

Test Case 4: All Numbers are Equal

Enter the first number: 6
Enter the second number: 6
Enter the third number: 6
The largest number is 6

Handling Edge Cases

  1. Negative Numbers: The program works with both positive and negative numbers. Example:
   Enter the first number: -3
   Enter the second number: -7
   Enter the third number: -1
   The largest number is -1
  1. Decimal Numbers: Since we used float() to accept input, the program handles decimal numbers without any issues. Example:
   Enter the first number: 4.5
   Enter the second number: 7.2
   Enter the third number: 6.8
   The largest number is 7.2
  1. Equal Numbers: If all three numbers are equal, the program will still work correctly and output the number since they are all the same.

Enhancing the Program with Exception Handling

If you’d like to handle invalid inputs (like entering non-numeric values), you can add a try-except block:

# Python program to find the largest of three numbers with error handling

try:
    # Input: Asking the user to enter three numbers
    num1 = float(input("Enter the first number: "))
    num2 = float(input("Enter the second number: "))
    num3 = float(input("Enter the third number: "))

    # Check which number is the largest
    if num1 >= num2 and num1 >= num3:
        print(f"The largest number is {num1}")
    elif num2 >= num1 and num2 >= num3:
        print(f"The largest number is {num2}")
    else:
        print(f"The largest number is {num3}")

except ValueError:
    print("Invalid input! Please enter a valid number.")

In this version, the program checks if the input is valid, and if it isn’t, the except block prints a friendly error message.


Conclusion

Finding the largest of three numbers is a simple but common problem that helps solidify your understanding of comparison operators and conditional statements in Python. By using if, elif, and else, you can easily compare three numbers and determine the largest.

We also explored how to handle decimal and negative inputs, and how to enhance the program with error handling for invalid inputs. This exercise is an excellent stepping stone to more complex decision-making problems in programming.

Keep practicing these basic concepts—they form the foundation of more advanced programming tasks!


By mastering such simple programs, you’ll become more comfortable with Python syntax and logic, setting you up for more complex challenges ahead. Happy coding!

Share with our team

Leave a Comment