Python program to find the greatest of three numbers

Python program to find the greatest of three numbers

In this tutorial you will learn Python program to find the greatest of three numbers using three approaches like if elif, using list and built-in function max(). Let’s learn these approaches with examples.

Python program to find the greatest of three numbers

Given three numbers a,b and c. The task is to find the greatest of three numbers using Python programming language.

Python program to find the greatest of three numbers

 

Method 1: Using if elif else

In the below program, the three numbers are stored in a,b and c variables. And we are going to use if elif else logic to find the greatest among three numbers and print the output.

For example, the following are the given numbers.

a=5
b=10
c=7

Below is the Python program to find the largest of three given numbers.

>>> def maximum(a,b,c):    
...    if (a>=b) and (a>=c):
...        greatest = a
    
...    elif (b>=a) and (b>=c):
...        greatest = b
    
...    else:
...        greatest = c

...    return greatest

Let’s try to call maximum method and print the output.

>>> print(maximum(a,b,c))

Output

10

Using List and max() built-in function

The below example, initializes three variables x,y and z with values 5,10 and 7. Then add those three numbers in to a list. Finally, you can find the largest number using built-in python function max() as shown below.

>>> x=5 
>>> y=10
>>> z=7 >>> def maximum(x,y,z): ... list=[x,y,z] ... return max(list) >>> maximum(x,y,z) 10

Using max() built-in function

In this approach, you will be using only the max() built-in function. And you need to pass numbers as arguments and it return the largest item of two or more arguments as shown below.

>>> max (x,y,z)
10

That’s it. You have learnt how to find the greatest of three given numbers using Python programming using different approaches.

Hope it helped 🙂

You’ll also like:

References

guest
0 Comments
Inline Feedbacks
View all comments