02. Write A Python Program to Find Largest Number Among 3 Numbers

Introduction :

Hello friends, in today’s post I am going to tell you how you can find the largest number out of 3 numbers in Python and that too very easily. read till and read carefully so that you understand everything properly.

Python Code for Find Largest Number Among 3 Numbers :

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

if (num1 >= num2) and (num1 >= num3):
   largest = num1
elif (num2 >= num1) and (num2 >= num3):
   largest = num2
else:
   largest = num3

print("The largest number is", largest)

Explanation :

The above Python program is used to find the largest number among three numbers. The program starts by defining three variables num1, num2, and num3 to store the input numbers. The input function is used to take the numbers as input from the user, and the float function is used to convert the input from string to float so that it can take decimal values as well.

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

After the input, the program uses the if-elif-else statement to compare the numbers and find the largest one. The first if statement checks if num1 is greater than or equal to num2 and num1 is greater than or equal to num3. If this condition is true, then num1 is the largest number, and the program assigns the value of num1 to the variable largest.

if (num1 >= num2) and (num1 >= num3):
   largest = num1

The second elif statement checks if num2 is greater than or equal to num1 and num2 is greater than or equal to num3. If this condition is true, then num2 is the largest number, and the program assigns the value of num2 to the variable largest.

elif (num2 >= num1) and (num2 >= num3):
   largest = num2

If both of the above conditions are false, then num3 is the largest number, and the program assigns the value of num3 to the variable largest using the else statement.

else:
   largest = num3

Finally, the program prints the result using the print function.

print("The largest number is", largest)

This program will take three numbers as input from the user and compares the numbers to find the largest one and finally prints the result.

Summary :

The above Python program takes three numbers as input from the user and finds the largest number among them. It uses the if-elif-else statement to compare the numbers and assigns the value of the largest number to the variable largest. Finally, it prints the result using the print function. The input is taken as float so that it can take decimal values as well. The program prompts the user to enter three numbers, then it compares the numbers to find the largest one and finally prints the result.

Leave a Reply

Your email address will not be published. Required fields are marked *