write a python program to find and display the maximum of three given numbers

In programming, there are often situations where we need to find the largest value among a set of numbers. 

Python provides a simple and efficient way to accomplish this task. In this blog, we will focus on writing a Python program to find and display the maximum of three given numbers. 

I will guide you through the process step-by-step, allowing you to understand the logic behind the code.

Title :  Write a Python Program to Find And Display The Maximum of Three Given Numbers


Accepting User Input


To begin, we will prompt the user to enter three numbers that we will compare to find the maximum value. We can use the input() function to obtain these numbers. Let's take a look at the code snippet:



num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
 




In the code above, we use the input() function to accept user input for each number. The float() function is used to convert the input to a floating-point number, allowing us to handle decimal values as well.



Finding the Maximum Number


After accepting the user's input, we will determine the maximum value among the three numbers. Python provides the max() function, which takes multiple arguments and returns the largest value. 

However, for the purpose of this exercise, we will use an if-else statement to find the maximum value without using the built-in max() function. Here's the code snippet:




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



In the code above, we use the if-else statement to compare the three numbers. We check each number against the other two using the greater than or equal to (>=) operator. 

If a number is greater than or equal to both of the others, it is assigned as the maximum value. We repeat this process for the remaining numbers, updating the maximum variable accordingly.



Displaying the Maximum Number


Now that we have determined the maximum value, we can display it to the user. Let's add the following code snippet:


print("The maximum of the three numbers is:", maximum)
 


In the code above, we use the print() function to display the maximum value to the user. We concatenate the output message with the value of the maximum variable using the comma (,) separator.


Final Code



num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

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

print("The maximum of the three numbers is:", maximum)

 

 

Output 1: 


Enter the first number: 10.5
Enter the second number: 8.2
Enter the third number: 12.7
The maximum of the three numbers is: 12.7

 

Output 2:



Enter the first number: 25
Enter the second number: 41
Enter the third number: 33
The maximum of the three numbers is: 41.0

 



Conclusion:


In this blog, we explored how to write a Python program to find and display the maximum of three given numbers. We began by accepting user input for the three numbers, then used an if-else statement to compare the values and determine the maximum.

Finally, we displayed the maximum value to the user. By following these steps, you can easily find the largest number among any set of three values. 

We hope this guide has been helpful in expanding your knowledge of Python programming.

Post a Comment

0 Comments