ScanSkill

Find The Largest Number Among Three Numbers in Python

This example programs are to find the largest number among three numbers in python. int() is used for integer type conversion in these examples. This covers three different methods to find the largest one:

  • Find the largest number among three numbers using if only
  • Find the largest number among three numbers using nested if-else
  • Find the largest number using a function

Prerequisites

Examples

Here are the different examples with different approaches to find the largest number among three numbers in python:

Find the largest number among three numbers using if only

This is the shortest program to find the largest number among three numbers. In this, three numbers num1, num2 and num3 are taken from user input, and if-condition is applied to find the largest number.

print("Enter three Numbers... ")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))

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

Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.

Output:

Enter three Numbers... 
Enter first number: 12
Enter second number: 52
Enter third number: 47
The largest number among 12, 52 and 47 is 52.

Find the largest number among three numbers using nested if-else

The following code is to find the largest number among three numbers using a nested if-else statement. In this, three numbers from the user input are used in if-else conditions to find the largest one.

print("Enter three Numbers... ")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))

if num1 > num2:
    if num2 > num3:
        large_num = num1
    else:
        if num3 > num1:
            large_num = num3
        else:
            large = num1
else:
    if num2 > num3:
        large_num = num2
    else:
        large_num = num3

print(f"The largest number among {num1}, {num2} and {num3} is {large_num})

Output:

Enter three Numbers... 
Enter first number: 12
Enter second number: 52
Enter third number: 24
The largest number among 12, 52 and 24 is 52

Find the largest number among three numbers using a function

In the following example, we created a function named find_largest() which takes three arguments to return the largest number. All three numbers are taken from user input.

def find_largest(n1, n2, n3):
    if n1 > n2:
        if n2 > n3:
            return n1
        else:
            if n3 > n1:
                return n3
            else:
                return n3
    else:
        if n2 > n3:
            return n2
        else:
            return n3

print("Enter three Numbers... ")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))

large = find_largest(num1, num2, num3)

print(f"The largest number among {num1}, {num2} and {num3} is {large}")

Output:

Enter three Numbers... 
Enter first number: 12
Enter second number: 55
Enter third number: 33
The largest number among 12, 55 and 33 is 55

Conclusion

In this, we talked about different examples with different approaches to find the largest number among the three numbers in python.