Python program to find largest of 4 numbers using if else

In this program, the user is prompted to enter 4 numbers one by one. The program uses if-else statements to compare the numbers and find the largest one. It compares the first number with the second, third, and fourth numbers using if statement. If the first number is greater than the second, third and fourth numbers, it is stored as the largest number. If the first number is not the largest, then the program compares the second number with the third and fourth numbers using elif statement, if the second number is greater than the third and fourth numbers, it is stored as the largest number. If the second number is not the largest, then the program compares the third number with the fourth number using elif statement, if the third number is greater than the fourth number, it is stored as the largest number. If none of the above conditions met then the fourth number is the largest one. Finally, the program prints the largest number that was found.

# Find largest of 4 numbers using if-else

# Prompt user to enter the first number
num1 = int(input("Enter the first number: "))

# Prompt user to enter the second number
num2 = int(input("Enter the second number: "))

# Prompt user to enter the third number
num3 = int(input("Enter the third number: "))

# Prompt user to enter the fourth number
num4 = int(input("Enter the fourth number: "))

# Compare the first number with the second, third, and fourth numbers
if num1 > num2 and num1 > num3 and num1 > num4:
    largest = num1

# Compare the second number with the third and fourth numbers
elif num2 > num3 and num2 > num4:
    largest = num2

# Compare the third number with the fourth number
elif num3 > num4:
    largest = num3
else:
    largest = num4

# Print the largest number
print("Largest number is:", largest)

Leave a Comment