Write a program to find the largest of three numbers in Python
In this post, you will learn how to find the largest of three numbers using the Python programming language. 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.
Find greatest number using if-else statement in Python
In the given Python program, we have taken inputs from the user and stored them in three consecutive variables. By using if..elif..else statement, we are finding the greatest among three numbers.
# 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):
greatest = 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. In Python, max() is a built-in function that we can use to find the largest number in an iterable.
# 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 used a user-defined function to find the largest of three numbers using the Python programming language.
# 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
Related Articles
Python Program to Check if a Number is Odd or Even
Python program to multiply two numbers
Prime factors of a number in Python
Find square of a number in Python
Python program to add two numbers
Python program to input week number and print week day
Find average of n numbers in Python
Python program for Prime Number
Remove element from list Python
Python split multiple delimiters
Python loop through list
Power of a number using recursion in Python
Python add list to list
Python check Palindrome strings and numbers
Python print odd numbers within a given range
Python random choice
Python dict inside list
Greatest common divisor (GCD) in Python
Python raise keyword