• > 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
- Start
- Take two numbers as input from the user
- Compare the first number with the second
- If the first number is greater than the second, it is the largest
- Print first number is greater
- Display the largest number
- Else display the second number
- 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
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.
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.
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.
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.
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.
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.
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().
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.