Python Program to Find the Largest Among Three Numbers


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co

To find the largest among three numbers, you can compare them using some logic and conditions1234. For example, if you have three numbers A, B and C, you can follow these steps:

Check if A is greater than B. If yes, then check if A is greater than C. If yes, then A is the largest number. If no, then C is the largest number. If A is not greater than B, then check if B is greater than C. If yes, then B is the largest number. If no, then C is the largest number.


Python Code :

The below Python program finds the largest among three numbers:


# Get input numbers from user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Find the largest number
if num1 >= num2 and num1 >= num3:
    largest = num1
elif num2 >= num1 and num2 >= num3:
    largest = num2
else:
    largest = num3

# Print the result
print("The largest number is", largest)

In this program, the user is prompted to enter three numbers. The program then checks which number is the largest using a series of if and elif statements.

The program checks whether :

  1. num1 is greater than or equal to num2 and num3 using the expression num1 >= num2 and num1 >= num3.

  2. Then if num1 is the largest number. Otherwise, the program checks whether num2 is greater than or equal to num1 and num3 using the expression num2 >= num1 and num2 >= num3.

  3. else if num2 is the largest number.

  4. Otherwise, num3 is the largest number.

Finally, the program prints out the result using the print function.

Note that this program assumes that the user enters valid floating-point numbers. If the user enters invalid input, such as a string or a non-numeric value, the program will raise a ValueError or produce an incorrect result.

Also, note that the program works only for three numbers. If you want to find the largest among more than three numbers, you need to modify the program accordingly.

For example :


Enter the first number: 25
Enter the second number: -100
Enter the third number: 250
The largest number is 250.0



10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co