Write a Python program to find the smallest of 3 numbers using if..elif.
Answers
Read 3 input numbers using input() or raw_input().
Use two functions largest() and smallest() with 3 parameters as 3 numbers
largest(num1, num2, num3)
check if num1 is larger than num1 and num2, if true num1 is largest, else
check if num2 is larger than num1 and num3, if true num2 is largest,
if both the above fails, num3 is largest
Print the largest number
smallest(num1, num2, num3)
check if num1 is smaller than num1 and num2, if true num1 is smallest, else
check if num2 is smaller than num1 and num3, if true num2 is smallest,
if both the above fails, num3 is smaller
Print the smallest number
Program :
number1 = int(input('Enter First number : '))
number2 = int(input('Enter Second number : '))
number3 = int(input('Enter Third number : '))def largest(num1, num2, num3):
if (num1 > num2) and (num1 > num3):
largest_num = num1
elif (num2 > num1) and (num2 > num3):
largest_num = num2
else:
largest_num = num3
print("The largest of the 3 numbers is : ", largest_num)def smallest(num1, num2, num3):
if (num1 < num2) and (num1 < num3):
smallest_num = num1
elif (num2 < num1) and (num2 < num3):
smallest_num = num2
else:
smallest_num = num3
print("The smallest of the 3 numbers is : ", smallest_num)largest(number1, number2, number3)
smallest(number1, number2, number3)
Answer:
hello
Get three inputs num1, num2 and num3 from user using input() methods. Check whether num1 is smaller than num2 and num3 using if statement, if it is true print num1 is smallest using print() method. Else, num2 or num3 is smallest. So check whether num2 is smaller than num3 using elseif statement.