Python – Largest of Three Numbers

Python – Find Largest of Three Numbers

To find the largest of three numbers, we could write a compound condition to check if a number is greater than other two.

Example 1: Find Largest of Three Numbers using IF

In this example, we shall use simple Python If statement to find the largest of three numbers.

We shall follow these steps to find the largest number of three.

  1. Read first number to a.
  2. Read second number to b.
  3. Read third number to c.
  4. If a is greater than b and a is greater than c, then a is the largest of the three numbers.
  5. If b is greater than a and b is greater than c, then b is the largest of the three numbers.
  6. If c is greater than a and c is greater than b, then c is the largest of the three numbers.

Python Program

a = int(input('Enter first number  : '))
b = int(input('Enter second number : '))
c = int(input('Enter third number  : '))

largest = 0

if a > b and a > c:
    largest = a
if b > a and b > c:
    largest = b
if c > a and c > b:
    largest = c

print(largest, "is the largest of three numbers.")

Output

Example 2: Find Largest of Three Numbers using IF-ELIF

In our previous example, we have written conditions to find the largest. But, these conditions are independent of each other. And we have not taken advantage that if a is not greater than either of b or c, then there is no use to check if b is greater than a or c is greater than a.

So, using elif statement, we shall take advantage of the dependency of conditions.

Python Program

a = int(input('Enter first number  : '))
b = int(input('Enter second number : '))
c = int(input('Enter third number  : '))

largest = 0

if a > b and a > c :
    largest = a
elif b > c :
    largest = b
else :
    largest = c

print(largest, "is the largest of three numbers.")

Output

Python - Find Largest of Three Numbers

Summary

In this tutorial of Python Examples, we learned how to find the largest of three numbers using conditional statements.