In this post, I will be discussing how to write a Python Program to Find Largest or Greatest among three Numbers.
I will be discussing three ways to write the python program.
- Using if-else statement
- Using the list and inbuilt function
Let's discuss these ways one by one.
Python Program to Find Largest or Greatest among three Numbers
Using if-else statement (Method 1)
In this program, we will be using the if-else statement.
Firstly, a number is compared to other two numbers and then another number is compared to other two numbers and if both the comparisons fail then the body of else statement is executed.
Code:-
# taking first number
num1 = int(input("Enter first number: "))
# taking second number
num2 = int(input("Enter second number: "))
# taking third number
num3 = int(input("And Enter third number: "))
if (num1 > num2) and (num1 > num3):
max = num1
elif (num2 > num1) and (num2 > num3):
max = num2
else:
max = num3
# printing the max
print("The largest among these three numbers is",max)
Output:-
Enter first number: 24
Enter second number: 68
And Enter third number: 35
The largest among these three numbers is 68
Using if-else statement (Method 2)
In this method also I will be using the if-else statement. But in this program code I will declare a variable max and initialize to variable num1 and then I will be comparing this max variable with other left variables.
Code:-
# taking first number
num1 = int(input("Enter first number: "))
# taking second number
num2 = int(input("Enter second number: "))
# taking third number
num3 = int(input("And Enter third number: "))
# assuming first number to max
max = num1
# checking if num2 is greater than max
if(num2 > max):
# assigning num2 to max
max = num2
# checking if num3 is greater than max
elif(num3 > max):
# assigning num3 to max
max = num3
else:
# assigning num1 to max
max = num1
# printing the max
print("The greatest among these three numbers is",max)
Output:-
Enter first number: 568
Enter second number: 425
And Enter third number: 742
The greatest among these three numbers is 742
Using the list and inbuilt function
In this program code, I will make the list of the three numbers and then use inbuilt max() function. I will pass the list inside the max() function and it will return the greatest number of the three numbers.
Code:-
# taking first number
num1 = int(input("Enter first number: "))
# taking second number
num2 = int(input("Enter second number: "))
# taking third number
num3 = int(input("And Enter third number: "))
# creating list of numbers taken
ls = [num1, num2, num3]
# printing the max
print("The largest among these three numbers is",max(ls))
# uncomment the below line to execute it
# print("The largest among these three numbers is",max(num1, num2, num3))
Output:-
Enter first number: 86547
Enter second number: 42156
And Enter third number: 235416
The largest among these three numbers is 235416
In this python program, we need not make a list, we can directly pass these numbers as the parameters to max() function and it will return the greatest number among them.
Hence we have discussed all the ways. If you find any problem you can comment below.