Write a Python program to find the largest number among three numbers provided by the user.
Input-Output Examples
plaintext
Example 1:
Input:
Enter first number: 5
Enter second number: 10
Enter third number: 7
Output:
The largest number is: 10
Example 2:
Input:
Enter first number: -3
Enter second number: -1
Enter third number: -2
Output:
The largest number is: -1
Algorithm to find the largest number among three numbers
- Start
- Take input: Prompt the user to enter three numbers.
- Check the largest number:
- Compare the first number with the second and third.
- Determine the largest number using conditional statements.
- Display the result: Print the largest number.
- End
Python Program to find the largest number among three numbers
python
# Python program to find the largest among three numbers
# Take input from the user for three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Determine the largest number
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
# Display the result
print(f"The largest number is: {largest}")
Code Explanation
- Taking input from the user:
Theinput()
function is used to take three numbers from the user. The input values are converted tofloat
to handle both integers and decimals. - Finding the largest number:
The program uses conditional statements (if-elif-else
) to compare the three numbers:- If the first number is greater than or equal to both the second and third numbers, the first number is the largest.
- If the second number is greater than or equal to both the first and third numbers, the second number is the largest.
- Otherwise, the third number is the largest.
- Displaying the output:
Theprint()
function displays the largest number using an f-string to format the result clearly.