In this article, we will create a Python program that takes three numbers and finds the largest number among them using the if-else statement.
Let’s dive in and explore how we can print the largest value among three given numbers in Python!
Required Knowledge
- Variables and Data Types
- Comparison Operators
- If-Else Statements
- Basic Arithmetic Operations
Problem Statement
Write a Python program that finds the maximum among three given numbers using the if-else statement. Your program should take three input numbers from the user and then determine and display the largest number among them.
Example:
Input: 5 8 9
Output: The largest number is 9
Python Program to Find the Greatest Number Among Three Given Numbers Using If-Else
\# Prompt the user to enter three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
\# Compare the three numbers using if-else statements
if num1 >= num2 and num1 >= num3:
max_num = num1
elif num2 >= num1 and num2 >= num3:
max_num = num2
else:
max_num = num3
\# Display the maximum number
print("The largest number is:", max_num)
Output:
Enter the first number: 8
Enter the second number: 15
Enter the third number: 4
The largest number is: 15
Explanation of the program
In the first step, the program prompts the user to enter three numbers using the input() function. As we want to take the input in numbers, we give the input() function a float value by using syntax: float(input())
.
It is done to convert the input() into floating-point numbers, allowing calculation of decimal values.
Each number provided as input are stored in separate variables: num1
, num2
, and num3
, as shown below:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
/# The above code creates an input prompt to take three input from the user
In the second step, we compare the three numbers using if-else statements. The conditional statements compare the three numbers and returns the largest one.
For example:
- If
num1
is greater than or equal tonum2
andnum3
, the program assigns it to a variablemax_num
. - Likewise, if
num2
is greater than or equal tonum1
andnum3
, the program assignsnum2
to variablemax_num
. - If none of the above conditions meet the criteria, it means that
num3
is the largest, and it is assigned tomax_num
.
if num1 >= num2 and num1 >= num3:
max_num = num1
elif num2 >= num1 and num2 >= num3:
max_num = num2
else:
max_num = num
In the last step, the program prints the largest number using the print() function. This way we can display the largest number among the three given numbers and show a relevant text with it.
print("The largest number is:", max_num)
That’s it! We have created the Python program that compares three numbers using if-else statements. After comparing the three numbers, it displays the largest number and displays an appropriate message with it.
Conclusion
In this article, we learned how to write a Python program that finds the largest number among three given numbers. The program prompts the user to input three numbers, and then compares them using if-else statements, and finally displays the maximum value.
By understanding and applying this program, you have gained valuable knowledge on using conditional statements in Python to solve practical problems. Try it yourself by giving different inputs and checking the results.
Happy Learning.