Largest of three numbers

EasyIf-Else Programs · 14 of 64

Write a Python program to find the largest of three numbers.

Advertisement
Input
a = 45, b = 89, c = 23
Output
Largest: 89

Approach

  1. Compare each number with the other two using >=.

Solution 1: if-elif

Python
a, b, c = 45, 89, 23 if a >= b and a >= c:    largest = aelif b >= a and b >= c:    largest = belse:    largest = cprint("Largest:", largest)
Output
Largest: 89

Line by line

Line 3

a is the largest if it is at least as big as both others. >= handles equal values.

Lines 7-8

If neither a nor b is the largest, it must be c.

Solution 2: max()

Python
print(max(45, 89, 23))
Output
89
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs