Python program to find the largest among 3 numbers. We will discuss different methods to find the largest of 3 numbers. We will give three numbers num1, num2, and num3. Python program will find the greatest of three numbers using various methods.
How to find largest of three numbers.
- If num1 >= num2 and num3 then num1 is largest number.
- else if num2 >= num1 and num3 then num2 is largest number.
- else num3 will be >= num1 and num2 and therefore num3 is largest number.
Mathematically,
num1 = 5, num2 = 8, and num3 = 7
Output:- 8 is the largest number.
Now, let’s see how we can find the largest of three numbers in python.
Python Program to Find the Largest among Three Numbers
This is the simplest and easiest way to find the largest among three numbers program in Python. We will take three numbers while declaring the variables. Find the largest number using the if-else statement and its value will be stored to the largest variable. Finally, it will be displayed on the screen.
Program description:- Write a Python program to find largest of three numbers
# Python program to find largest of 3 numbers
# take inputs
num1 = 5
num2 = 3
num3 = 9
# find largest numbers
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
# display result
print('The largest number = ', largest)
Output:-
The largest number = 9
In this program, we have hardcoded the values of numbers num1, num2, and num3 in the source code, 5, 3, and 9 numeric values are assigned to them.
num1 = 5
num2 = 3
num3 = 9
Find the largest number using if-else statement.
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
Finally, display the largest number of those numbers using print() function.
print('The largest number = ', largest)
Write a Program to Find the largest of Three Numbers in Python
In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.
# Python program to find the largest of three numbers
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
# find largest numbers
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
# display result
print('The largest number = ', largest)
Output for the different input values:-
Enter first number: 7
Enter second number: 15E
nter third number: 4
The largest number = 15.0
Enter first number: 25
Enter second number: 23E
nter third number: 97
The largest number = 97.0
In this program, inputs are scanned using the input() function and stored in variable num1, num2, and num3.
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
Find the largest number using the if-else statement and Finally, display the largest number of those numbers using the print() function.
Largest among Three Numbers using max() Function
In this program, We can find the largest number of three numbers using the max() function. max() function will be imported from the library function.
# Python program to find the largest among
# three numbers using max() function
# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
# find largest numbers and display result
print('The largest number = ', max(num1, num2, num3))
Output:-
Enter first number: 15
Enter second number: 10E
nter third number: 19
The largest number = 19.0
Program to Find Greatest of Three Numbers in Python using Function
We can also take the help of a function to find the average of 3 numbers in python. A function is a block of code that performs a specific task.
# Python program to find greatest of three numbers using function
def findLargest(num1, num2, num3): #user-defined function
# 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 inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))
num3 = float(input('Enter third number: '))
# function call
maximum = findLargest(num1, num2, num3)
# display result
print('The largest number = ',maximum)
Output:-
Enter first number: 4
Enter second number: 8E
nter third number: 7
The largest number = 8.0
In this program, we will be the first defined function.
def findLargest(num1, num2, num3): #user-defined function
# 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
Inputs are scanned using the input() function and stored in variable num1, num2 and num3. Then call the function and print the largest number of those numbers.
Also See:- Find the Average of 3 Numbers in Python
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!