Find the Largest Among Three Numbers

AIM

To write a Python program that finds the largest among three numbers entered by the user using conditional statements.

ALGORITHM

  1. Start
  2. Take three numbers as input from the user
  3. Compare the first number with the second and third
  4. If the first number is greater than both, it is the largest
  5. Otherwise, compare the second number with the third
  6. Display the largest number
  7. Stop

PROGRAM

# Python program to find the largest among three numbers

# Input three numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))

# Finding largest using if-elif-else
if num1 >= num2 and num1 >= num3:
    print(f"Largest number is: {num1}")
elif num2 >= num1 and num2 >= num3:
    print(f"Largest number is: {num2}")
else:
    print(f"Largest number is: {num3}")

# Alternative method using max() function
# largest = max(num1, num2, num3)
# print(f"Largest number is: {largest}")

OUTPUT

Sample Output 1:

========================================

Enter first number: 25

Enter second number: 40

Enter third number: 15

Largest number is: 40


Sample Output 2:

========================================

Enter first number: 50

Enter second number: 30

Enter third number: 50

Largest number is: 50

CONCLUSION

Thus, the given program was successfully executed and the output was verified as per the expected result. The program successfully finds the largest among three numbers using conditional statements.

VIVA QUESTIONS

  1. What are conditional statements in Python?

    Conditional statements are used to execute different blocks of code based on certain conditions. Python uses if, elif, and else statements.

  2. What is the difference between = and == operators?

    = is the assignment operator used to assign values to variables, while == is the equality operator used to compare two values.

  3. What are logical operators in Python?

    Logical operators are 'and', 'or', and 'not'. They are used to combine conditional statements and return True or False.

  4. What would happen if we input non-numeric values?

    If non-numeric values are entered, the program will raise a ValueError because the int() function cannot convert strings to numbers.

  5. How does the max() function work?

    The max() function returns the largest item from an iterable or the largest of two or more arguments. It can handle any number of arguments.

  6. What is the advantage of using nested if statements?

    Nested if statements provide more control over the decision-making process and can handle complex conditions, though they may be less readable.

  7. How can you modify this program to find the smallest number?

    Change the comparison operators from >= to <= or use the min() function instead of max().

  8. What is the purpose of the int() function?

    The int() function converts a string or float to an integer number, allowing whole number values to be processed.