Python Program to find the largest among three numbers

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

  1. Start
  2. Take input: Prompt the user to enter three numbers.
  3. Check the largest number:
    • Compare the first number with the second and third.
    • Determine the largest number using conditional statements.
  4. Display the result: Print the largest number.
  5. 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

  1. Taking input from the user:
    The input() function is used to take three numbers from the user. The input values are converted to float to handle both integers and decimals.
  2. 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.
  3. Displaying the output:
    The print() function displays the largest number using an f-string to format the result clearly.

Python

4734

608

Related Articles