Beginner Number Problems Python

Largest of Three Numbers in Python

Find the largest of three numbers in Python using if-elif-else and using the built-in max() function.

  • TimeO(1)
  • SpaceO(1)
  • Hints3
  • Read3 min
Quick answer

To find the largest of three numbers a, b and c in Python, check if a >= b and a >= c, then a is largest; elif b >= c, then b is largest; else c is largest. Use >= rather than > so that equal values are handled. The one-line way is max(a, b, c).

if-elif-elseComparison operatorsLogical andmax() and min()
1

Problem statement

Write a Python program to find the largest of three numbers. Read three numbers and print the greatest one.

Write it with if-elif-else first, then with max(). Make sure it works when two or all three numbers are equal.

Input

Three numbers a, b and c.

Output

The largest of the three.

Examples

Example 1
Inputa = 10, b = 25, c = 7
Output25

25 is greater than both 10 and 7.

Example 2
Inputa = 5, b = 5, c = 9
Output9

9 is greater than both 5s.

Example 3
Inputa = 8, b = 8, c = 8
Output8

All three are equal, so the largest is 8.

Constraints

  • The numbers can be negative, zero or decimal
  • Two or all three numbers can be equal
2

Think before you code

Reveal the questions to ask yourself first
  • What two comparisons prove that a is the largest?
  • If a is not the largest, how many comparisons do you still need?
  • What should happen when a and b are equal and bigger than c?
3

Hints

Open one at a time — try again after each.
1 Hint 1
a is the largest if a >= b and a >= c. Join the two checks with and.
2 Hint 2
If a is not the largest, the answer is b or c, so a single b >= c decides it.
3 Hint 3
Use >=, not >. With > and inputs like 5, 5, 3, every condition fails.
4

Approach

Reveal the step-by-step approach

Greatest of three numbers in Python with if-elif

  1. If a >= b and a >= c, a is the largest.
  2. elif b >= c, b is the largest. (We already know a is not.)
  3. else, c is the largest.

Only one branch runs, from top to bottom. As soon as one condition is True, Python skips the rest.

Find maximum of three with max()

max(a, b, c) returns the largest value. It also works for any number of values, or a list: max([a, b, c, d]). min() does the opposite.

Method Lines When to use
if-elif-else 6 Exams and lab records that test conditions
Running maximum 5 When you will extend it to many numbers
max(a, b, c) 1 Real code, or when built-ins are allowed

More on conditions in if-else statements in Python.

5

Dry run

Walk through the example step by step

a = 10, b = 25, c = 7:

check                     | values             | result
--------------------------+--------------------+-------
if a >= b and a >= c      | 10 >= 25 is False  | skip
elif b >= a and b >= c    | 25 >= 10, 25 >= 7  | True
largest = b               |                    | 25

The else branch is never reached.

6

Solution

Reveal the full Python solution
solution.py
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

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

print("Largest number is", largest)

Output:

Enter first number: 10
Enter second number: 25
Enter third number: 7
Largest number is 25

The elif here checks b >= a again for clarity. Since we only reach it when a is not the largest, elif b >= c alone is enough, as in the function below.

Compare the if-elif version with max()

def largest_of_three(a, b, c):
    if a >= b and a >= c:
        return a
    elif b >= c:
        return b
    else:
        return c


tests = [(10, 25, 7), (40, 15, 30), (5, 5, 9), (8, 8, 8), (-3, -9, -1)]
for a, b, c in tests:
    print(a, b, c, "->", largest_of_three(a, b, c), max(a, b, c))
10 25 7 -> 25 25
40 15 30 -> 40 40
5 5 9 -> 9 9
8 8 8 -> 8 8
-3 -9 -1 -> -1 -1

Both methods agree for equal values and negative numbers.

Running maximum

This version is easy to extend to 4, 5 or 100 numbers.

a, b, c = 10, 25, 7
largest = a
if b > largest:
    largest = b
if c > largest:
    largest = c
print(largest)
25

The same idea, run over a list, is find the largest element in an array.

Decimal numbers with max() and min()

a, b, c = 12.5, 7.25, 12.75
print(max(a, b, c))
print(min(a, b, c))
12.75
7.25

To read decimals from the user, use float(input()) instead of int(input()).

Why > instead of >= is a bug

a, b, c = 5, 5, 3
if a > b and a > c:
    print("a is largest")
elif b > a and b > c:
    print("b is largest")
else:
    print("c is largest")
c is largest

Neither a > b nor b > a is true when they are equal, so the code falls through to else and wrongly picks 3. Using >= fixes it.

The Java version of this page is find the largest of three numbers.

TimeO(1)
SpaceO(1)
7

Review before the interview

Common mistakes

  • Using > instead of >=. With two equal largest values, every branch fails and the code picks the wrong number.
  • Writing if a > b > c only. It checks one order, and misses cases like a = 10, b = 3, c = 7.
  • Forgetting int() or float() around input(). Strings compare letter by letter, so "9" > "25" is True.
  • Using separate if statements instead of elif, so more than one branch can run and overwrite the answer.

Edge cases to test

  • All three equal: the first branch is True and the answer is that value.
  • Two equal largest values, like 5, 5, 3: >= returns 5 correctly.
  • Negative numbers: -3, -9, -1 gives -1.

Interview follow-ups

  • How would you find the smallest of three numbers with the same structure?
  • How would you find the second largest of three numbers?
  • How would you find the largest of n numbers entered by the user?

Keep practising

Practising Python for interviews?

CodeBegun’s Python Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Chat with us