Q:

Write a program to find the largest of three numbers in Python

0

Write a program to find the largest of three numbers in Python

In this exercise, you will learn how to find the largest of three numbers using Python. Such a type of programming can improve the thinking power of a new developer to tackle a given issue. There are different ways to find the largest of the three numbers.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Find the largest number using if-else statement

In the given Python program, we are taking input from the user. The user enters three numbers and program find the biggest among three numbers using an if..elif..else statement.

# Python program to find the greatest of three numbers
# using if-else statement

x1 = int(input("Enter 1st number: "))
x2 = int(input("Enter 2nd number: "))
x3 = int(input("Enter 3rd number: "))

if(x1 >= x2) and (x1 >= x3):
 largest = x1
elif(x2 >= x1) and (x2 >= x3):
 greatest = x2
else:
 greatest = x3
 
print("The greatest number is ",greatest )

Output of the above code - 

Enter 1st number: 53
Enter 2nd number: 23
Enter 3rd number: 56
The greatest number is 56

Find the largest number using max() function

Here, we have found the greatest of the three numbers by using the max() function.

# Python program to find the greatest of three numbers
# using max() function

x1 = int(input("Enter 1st number: "))
x2 = int(input("Enter 2nd number: "))
x3 = int(input("Enter 3rd number: "))

print(max(x1,x2,x3),"is the greatest number")

Output of the above code - 

Enter 1st number: 34
Enter 2nd number: 21
Enter 3rd number: 75
75 is the greatest number

Find the largest number using function

Here, we have defined a function to find the largest of 3 numbers in Python.

# Python program to find the greatest of three numbers
# using the user-defined function

def findLargestNumber(num1, num2, num3): 
    # find largest numbers
    if (num1 >= num2) and (num1 >= num3):
        largest = num1
    elif(num2 >= num1) and (num2 >= num3):
        largest = num2
    else:
        largest = num3
    return largest  #return value

# take user inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))

# function call
maximum = findLargestNumber(num1, num2, num3)

print(maximum, " is the greatest number")

Output of the above code - 

Enter first number: 36
Enter second number: 62
Enter third number: 16
62.0  is the greatest number

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How do you transpose a matrix using list comprehen... >>
<< Write a python program to find the area of a recta...