Python Program to Find Largest Among Three Numbers
|In this Python example, we will discuss how can we find the largest or the maximum number among the given three numbers. Let’s begin
1. How to find the Largest number?
When the user provides different numbers, then we need to calculate the largest number among all the numbers using different possible ways like using inbuilt functions, if-else ladder etc.
Example: Input : 3, 4, 6 Output : Largest Number is 6 Input : 9, 5, 1 Output : Largest Number is 9
Some of the topics which will be helpful for understanding the program implementation better are:
2. Finding The Largest Number Using In-Built Function
In Python, there is an inbuilt function max()
, which can easily return us the maximum value from the list of values we pass as parameters to it.
Let’s implement the code and see how it works.
#Python Program to find the largest number using max() function n1 = int(input("Enter the first number: ")) n2 = int(input("Enter the second number: ")) n3 = int(input("Enter the third number: ")) # We can also take the input in one line # n1, n2, n3 = input("Enter three numbers: ").split() print(max(n1, n2, n3), "is the largest among all the three numbers")
Output Enter the first number: 32 Enter the second number: 12 Enter the third number: 98 98 is the largest among all the three numbers
3. Finding The Largest Number Using Simple If Else Condition
In this method, we manually compare each of the numbers from the other numbers to find the maximum or the largest among them.
Let’s implement and find out the working of the code.
# Python Program to find the largest number Number using simple if else condition def max_of_all(n1, n2, n3): if n1 >= n2 and n1 >= n3: largest = n1 elif n2 >= n1 and n2 >= n3: largest = n2 else: largest = n3 return (largest, "is the the largest among all the three numbers") n1 = int(input("Enter the first number: ")) n2 = int(input("Enter the second number: ")) n3 = int(input("Enter the third number: ")) print(max_of_all(n1, n2, n3))
Output Enter the first number: 32 Enter the second number: 12 Enter the third number: 98 98 is the largest among all the three numbers
In this method, we have simply used if else condition and user-defined function.
In the above program,
- if
n1
is greater than bothn2
andn3
then it will be printed as largest - else if
n2
is greater than bothn1
andn3
thenn2
will be printed as largest - else
n3
will be the only largest number left.
4. Conclusion
In this article, we have learned how can we find the maximum or the largest number among the given three numbers using the max in-build function and simply using if else condition.
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?