You can make a program to find the largest of 3 numbers using proper logic of if elif and else block in Python. This is a conditional statement.
- Use an if-else statement, If (num1 > num2) and (num1 > num3), Print num1
- Else if (num2 > num1) and (num 2 > num3), Print num2
- Else, print num3
- End the program
Python program to find the largest of 3 numbers example
A simple example code finds the largest number among the three input numbers.
def maximum(a, b, c): if (a >= b) and (a >= c): largest = a elif (b >= a) and (b >= c): largest = b else: largest = c return largest res = maximum(2, 3, 1) print("The largest number is", res)
Use input numbers
Program to Find the Largest Among Three Numbers, where numbers are dynamically entered by the user.
# take three numbers from user num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) num3 = float(input("Enter third number: ")) if (num1 > num2) and (num1 > num3): largest = num1 elif (num2 > num1) and (num2 > num3): largest = num2 else: largest = num3 print("The largest number is", largest)
Enter first number: 10
Enter second number: 50
Enter third number: 100
The largest number is 100.0
Do comment if you have any doubts or suggestions on this Python program.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.