Example:
Input: a = 10, b = 4, c = 9 Output: 10 Input: a = 5, b = 12, c = 10 Output: 12
There are multiple ways to find the largest number among three and here we are discussing three of them in detail with Python Code.
Python Program to Find Maximum of Three Using if-else statement.
Below are the steps to follow:
STEP 1: Take three inputs a, b, and c from the user.
STEP 2: Use the if-else statement to compare all three numbers.
- If a is greater than or equal to b and c, set largest to a.
- Else if b is greater than or equal to a and c, set largest to b.
- Else, set largest to c.
STEP 3: Print the value of largest as an output on the console screen.
Python Code:
# Python program find largest of three number # Step 1: Input three numbers num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) num3 = float(input("Enter the third number: ")) # Step 2: Compare using if-else statements if num1 >= num2 and num1 >= num3: largest = num1 elif num2 >= num1 and num2 >= num3: largest = num2 else: largest = num3 # Step 3: Output the result print(f"The largest number is: {largest}")
Enter the first number: 10 Enter the second number: 15 Enter the third number: 8 The largest number is: 15.0
Python Program to Find Maximum of Three Numbers Using max() Function.
Below are the steps to follow:
- STEP 1: Take three inputs a, b, and c from the user.
- STEP 2: Use the max() function to find the maximum among the three numbers. Assign the result to largest.
- STEP 3: Print the result, indicating the largest number.
Python Code:
# Python program find largest of three number using max() # Input three numbers num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) num3 = float(input("Enter the third number: ")) # Use the max() function largest = max(num1, num2, num3) # Output the result print(f"The largest number is: {largest}")
Enter the first number: 12 Enter the second number: 10 Enter the third number: 15 The largest number is: 15.0
No comments:
Post a Comment