In: Computer Science
Write a program whose inputs are two integers, and whose output is the smallest of the two values.
Ex: If the input is: 7 15
output is: 7
passed through command line
*in python
Python Program to find smallest integer value
This program accepts two integers as input from user(num1 and num2)
In this solution, two methods are given to find smallest integer value from these inputs.
Method 1 :
Using If else statements : If condition checks whether num1 is smaller than num2, if so it will print 'Smallest of these two integers is num1' .
else it will print 'Smallest of these two integers is num2'
Method 2:
Using 'min' method : using the method min(num1,num2) , program finds the smallest value and prints directly to user .
Python Program using method 1
#PYTHON PROGRAM TO FIND SMALLEST INTEGER VALUE FROM TWO INPUTS FROM USER - Method 1
#Accepts inputs from user
num1 = int (input ("Enter your first number:")) #Gets the first
integer input from user and stores in num1
num2 = int (input ("Enter your second number: ")) #Gets the second
integer input from user and stores in num2
#Finding output by if else
#Checks num1 smaller than num2
if (num1 < num2):
#assign num1 to small
small = num1
else:
#assign num2 to small
small = num2
print ("Smallest of these two integers is", small)
Python Program using method 2
#PYTHON PROGRAM TO FIND SMALLEST INTEGER VALUE FROM TWO INPUTS FROM USER - Method 2
#Accepts inputs from user
num1 = int (input ("Enter your first number:")) #Gets the first
integer input from user and stores in num1
num2 = int (input ("Enter your second number: ")) #Gets the second
integer input from user and stores in num2
#Finding output using min method
print("\n Smallest of these two integers is",min(num1,num2))
#Prints output to user
Output Screen using method1
Output Screen using method2
Screen shot of Python code