Find the Largest Among Three Numbers

Python Program to Find the Largest Among Three Numbers

Given three numbers the task is to find the largest number among the three numbers.

Prerequisite:

Python IF…ELIF…ELSE Statements

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Examples:

Input:

number1 = 3
number2 = 5
number3 = 7

Output:

The largest number among the three numbers = 7

Input:

number1 = 9
number2 = 9
number3 = 2

Output:

The largest number among the three numbers = 9

Determine the largest of three numbers

Below are the ways to determine the largest among the three numbers:

Method #1 :Using Conditional Statements

The three numbers are stored in number 1, number 2, and number 3, in that order. We use the if…elif…else ladder to find and show the largest of the three.

Here we compare one number with other two numbers using if..elif…else ladder.

Below is the implementation:

# function which returns the largest number among the three numbers
def findLargest(number1, number2, number3):
  # comparing first number with other two numbers
    if (number1 >= number2) and (number1 >= number3):
        return(number1)
      # comparing second number with other two numbers
    elif (number2 >= number1) and (number2 >= number3):
        return(number2)
    else:
        return(number3)


number1 = 3
number2 = 5
number3 = 7
# passing the three numbers to find largest number among the three numbers
print("The largest number among the three numbers =",
      findLargest(number1, number2, number3))

Output:

The largest number among the three numbers = 7

Method #2: Using max function

We can directly use max function to know the largest number among the three numbers.

We provide the given three numbers as arguments to max function and print it.

Below is the implementation:

number1 = 3
number2 = 5
number3 = 7
# using max function to find largest numbers
maxnum = max(number1, number2, number3)
print("The largest number among the three numbers =",
      maxnum)

Output:

The largest number among the three numbers = 7

Method #3: Converting the numbers to list and using max function to print max element of list

Approach:

  • Convert the given number to list using [].
  • Print the maximum element of list using max() function

Below is the implementation:

number1 = 3
number2 = 5
number3 = 7
# converting given 3 numbers to list
listnum = [number1, number2, number3]
# using max function to find largest numbers from the list
maxnum = max(listnum)
print("The largest number among the three numbers =",
      maxnum)

Output:

The largest number among the three numbers = 7

Related Programs: