Python Program to Find Largest among Three Numbers

In this article, we will learn about python program to find largest among three numbers with examples. We will see how to use built-in functions, if-else condition to achieve our task.

Getting Started

The task is to find greatest among three numbers using python program. For example,

If a = 1.3, b = 4.3 and c = 6.5, then output should be c = 6.5

We can do so in below ways –

  1. Using simple if-else Condition
  2. Using nested if-else Condition
  3. Using max() Function
  4. Using list and max() Function
  5. Using max() Function and Unpacking
  6. Using sorted() Function
  7. Using for Loop

Using simple if-else Condition

We can simply use if-else condition to write python program to find largest among three numbers as shown below –

def findLargest(num1, num2, num3):
    if num1 >= num2 and num1 >= num3:
        return num1
    elif num2 >= num1 and num2 >= num3:
        return num2
    else:
        return num3

# Input three numbers
num1 = float(input("Enter 1st number: "))
num2 = float(input("Enter 2nd number: "))
num3 = float(input("Enter 3rd number: "))

result = findLargest(num1, num2, num3)
print(f"The largest number among {num1}, {num2}, and {num3} is {result}.")

Here,

  • We have defined a method findLargest which we will return greatest among three numbers.
  • Inside findLargest method, we have used if-else condition. At first, we are checking if num1 is greater than num2 and num3 or not. If yes, num1 is greatest among three numbers.
  • If no, we are checking if num2 is greater than num1 and num3 or not. If yes, num2 is greatest among three numbers.
  • If no, we can return num3 as greatest number.

Output:

Enter 1st number: 
23.1
Enter 2nd number: 
21.2
Enter 3rd number: 
65.8
The largest number among 23.1, 21.2, and 65.8 is 65.8.

Using nested if-else Condition

We can also use if-else condition as ternary operator in python. So, if we want to write above program using ternary operator in python, we can do so as shown below –

# Input three numbers
num1 = float(input("Enter 1st number: "))
num2 = float(input("Enter 2nd number: "))
num3 = float(input("Enter 3rd number: "))

result = num1 if num1 >= num2 and num1 >= num3 else (num2 if num2 >= num3 else num3)
print(f"The largest number among {num1}, {num2}, and {num3} is {result}.")

Output:

Enter 1st number: 
4.8
Enter 2nd number: 
5.9
Enter 3rd number: 
23.89
The largest number among 4.8, 5.9, and 23.89 is 23.89.

Using max() Function

We can also use max() function to write python program to find greatest among three numbers as shown below –

# Input three numbers
num1 = float(input("Enter 1st number: "))
num2 = float(input("Enter 2nd number: "))
num3 = float(input("Enter 3rd number: "))

result = max(num1, num2, num3)
print(f"The largest number among {num1}, {num2}, and {num3} is {result}.")

Here,

  • max() function takes three numbers as input and returning the greatest among them.
  • Finally, result is printed using print() function.

Output:

Enter 2nd number: 
6.5
Enter 3rd number: 
34.9
The largest number among 1.2, 6.5, and 34.9 is 34.9.

Using list and max() Function

We can also use max() function with python list and find maximum among three numbers as shown below –

# Take input three numbers from user
num1 = float(input("Enter 1st number: "))
num2 = float(input("Enter 2nd number: "))
num3 = float(input("Enter 3rd number: "))

numbers = [num1, num2, num3]
result = max(numbers)
print(f"The largest number among {num1}, {num2}, and {num3} is {result}.")

Here,

  • We are passing a list of numbers to max() function.
  • max() function will then find largest number present in list and return it.
  • Finally, result is printed using print() function.

Output:

Enter 1st number: 
4.8
Enter 2nd number: 
5.9
Enter 3rd number: 
23.89
The largest number among 4.8, 5.9, and 23.89 is 23.89.

Using max() Function and Unpacking

In earlier examples, we passed either list or comma separated numbers to max() function as parameter. We can also use unpacking concept in python to pass different parameters in max() function and re-write python program to find largest among three numbers as shown below –

# Take input three numbers from user
numbers = [float(input("Enter number {}: ".format(i+1))) for i in range(3)]

largestNumber = max(*numbers)
print(f"The largest number is {largestNumber}.")

Here,

  • At first, we are accepting three numbers as input from user and storing it in variable – numbers
  • After that, passing numbers in max() function.
  • Then, max() function will find largest among three numbers and return it. Returned value is stored in variable – largestNumber
  • After that, we print the result using print() function.

Output:

Enter number 1: 
23.1
Enter number 2: 
76.9
Enter number 3: 
34.6
The largest number is 76.9.

Using sorted() Function

We can also use sorted() function in python to write python program to find largest among three numbers as shown below –

# Input three numbers
num1 = float(input("Enter 1st number: "))
num2 = float(input("Enter 2nd number: "))
num3 = float(input("Enter 3rd number: "))

sortedNumbers = sorted([num1, num2, num3])
result = sortedNumbers[-1]
print(f"The largest number among {num1}, {num2}, and {num3} is {result}.")

Here,

  • sorted() function takes three numbers as parameter and returns the sorted numbers in ascending order.
  • We can get largest numbers using [-1] as it will return last number from list.
  • Result is then stored in result variable and printed using print() function.

Output:

Enter 1st number: 
2.6
Enter 2nd number: 
9898.56
Enter 3rd number: 
34.98
The largest number among 2.6, 9898.56, and 34.98 is 9898.56.

Using for Loop

We can also use for loop to find greatest numbers among three numbers.

# Take input three numbers from user
numbers = [float(input("Enter number {}: ".format(i+1))) for i in range(3)]

largestNumber = numbers[0]
for num in numbers:
    if num > largestNumber:
        largestNumber = num

print(f"The largest number is {largestNumber}")

Here,

  • We are using for loop and iterating list one by one.
  • In each iteration, we are checking if that number is greater than already stored greatest value, in largestNumber variable, or not.
  • If yes, then largestNumber value is replaced with current value.
  • If no, we move to next iteration.

Output:

Enter number 1: 
43.6
Enter number 2: 
34.7
Enter number 3: 
23.98
The largest number is 43.6

Thus, we have seen many ways to write python program to find largest among three numbers using if-else, using for loop, using built-in method sorted() and max().

Learn about python at official Doc

Leave a Reply