codingstreets
Search
Close this search box.

Python Program: Find the Greatest Number Among Three Numbers

cube-six-gambling-play-37534.jpeg
Photo by Pixabay on Pexels.com

In this article, you will learn how to find the greatest number among three integers in Python.  Learn two different methods, using the built-in max() function and a custom-defined def function, to compare the numbers and determine the largest one. Explore practical examples and gain insights into the logic behind each approach. Enhance your Python skills and master finding the greatest number efficiently.

Also let’s see another Python Project: Find Palindrome Number in Python

Table of Contents

max() function

Example: Using max() function.

				
					num1 = 10
num2 = 25
num3 = 5

greatest_number = max(num1, num2, num3)
print("The greatest number is:", greatest_number)

#output: The greatest number is: 25
				
			

Explanation:

We started with three variables num1, num2, and num3, and used the max() function to take multiple arguments (num1, num2, num3) and return the largest one. The max() function will compare all the values provided as arguments and return the largest one, which is then assigned to the greatest_number variable. Finally, we print the result.

conditional statements

Example: Using conditional statements.

				
					num1 = 25
num2 = 12
num3 = 30

if num1 >= num2 and num1 >= num3:
    greatest_number = num1
elif num2 >= num1 and num2 >= num3:
    greatest_number = num2
else:
    greatest_number = num3

print("The greatest number is:", greatest_number)

#output: The greatest number is: 30
				
			

Explanation:

We start with conditional statements to compare the three numbers num1, num2, and num3. The variable greatest_number is assigned the value of the largest number based on the conditions of if..else blocks. Finally, we print the result.

def function

Example: Using def function.

				
					def find_greatest_number(num1, num2, num3):
    if num1 >= num2 and num1 >= num3:
        return num1
    elif num2 >= num1 and num2 >= num3:
        return num2
    else:
        return num3

# Test the function with three integers
result = find_greatest_number(15, 30, 20)
print("The greatest number is:", result)

#output: The greatest number is: 30
				
			

Explanation:

We start with a function find_greatest_number() that takes three arguments (num1, num2, and num3) representing the integers to be compared. The function uses conditional statements to compare the numbers and returns the greatest number.

Now,  we find_greatest_number() function with the integers 15, 30, and 20 as arguments and store the result in the variable result. Finally, we print the result.

Example: Using def function.

				
					def number1(first_num):
    def number2(second_num):
        def number3(third_num):

            if (first_num > second_num) and (first_num > third_num):
                print("first number:",first_num)
            elif (second_num > first_num) and (second_num > third_num):
                print("second number:",second_num)
            else:
                print("third number:",third_num)
                
        number3(30)
    number2(49)
number1(11)

#output: second_number: 49
				
			

Explanation:

We start with defining three functions with names number1, number2, number3 respectively. Here, we call one function from the inside of another function. Each function takes an argument as an integer. The function uses conditional statements to compare the numbers and returns the greatest number.

Now,  we call each function with the integers 11, 49, and 30 as arguments and print the result using conditional statements.

Conclusion

We have explored two different methods to find the greatest number among three integers in Python. The first method involved using the built-in max() function, which efficiently compared the numbers and returned the largest one. The second method utilized a custom-defined def function with conditional statements to determine the greatest number.

Whether choosing the convenience of the max() function or the versatility of a custom function, you are now equipped with the tools to find the greatest number among three integers in Python with confidence. This knowledge will prove useful in various scenarios, allowing you to tackle similar challenges in your programming journey.

Recent Articles