Table of Contents
Problem Understanding
5 min
Conditional Logic
8 min
Implementation
12 min
Edge Cases
8 min
Testing
7 min
Operators

> greater than

< less than

• == equal to

Find the Largest Among Two Numbers

AIM

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

ALGORITHM

  1. Start
  2. Take two numbers as input from the user
  3. Compare the first number with the second
  4. If the first number is greater than the second, it is the largest
  5. Print first number is greater
  6. Display the largest number
  7. Else display the second number
  8. Stop

PROGRAM

# Python program to find the largest and smallest among two numbers

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

# Finding largest and smallest
if num1 > num2:
    print("Largest number is:", num1)
    print("Smallest number is:",num2)
elif num2 > num1:
    print("Largest number is:",num2)
    print("Smallest number is:",num1)
else:
    print("Both numbers are equal.")

OUTPUT

Sample Output 1

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

Enter first number: 25

Enter second number: 40

Largest number is: 40

Smallest number is: 25


Sample Output 2

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

Enter first number: 50

Enter second number: 50

Both numbers are equal.

CONCLUSION

Thus, the given program was successfully executed and the output was verified as per the expected result.

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.