Write a Python Program to Read Three Numbers And Find The Smallest Among Them

Given three numbers as a input to the program your task is to find the smallest among these numbers and print it on the console

It can be done using just two comparison

 

Write a Python Program to Read Three Numbers And Find The Smallest Among Them





Write a Python Program to Read Three Numbers And Find The Smallest Among Them

Take 3 variables first, second and third,  assuming these are the integer variables,  ask the user to enter these three variables using input function call and use the type conversion to integer as shown below.

first = int(input("Enter First Number : "))
second = int(input("Enter Second Number : "))
third = int(input("Enter Third Number : "))




Assume the first number as the smallest,  compare the second number with the  smallest number.  if the second number is less than the smallest set the smallest second.  then compare The third number with the smallest.  if the  third number is less than smallest, set the smallest as third.


Upon completion of if statement smallest will be MI store in the smallest among first second and third number

smallest = first
if second < smallest:
   smallest = second
if third < smallest:
   smallest = third




Once we get the smallest among the three numbers use the print function call with the string formatting and print the smallest among the three numbers.

print(f"Smallest among {first}, {second} and {third} is : {smallest}")




Final Program
=======================

first = int(input("Enter First Number : "))
second = int(input("Enter Second Number : "))
third = int(input("Enter Third Number : "))

smallest = first
if second < smallest:
   smallest = second
if third < smallest:
   smallest = third

print(f"Smallest among {first}, {second} and {third} is : {smallest}")




OUTPUT
================

Enter First Number : 23
Enter Second Number : 45
Enter Third Number : 12
Smallest among 23, 45 and 12 is : 12




OUTPUT
==================

Enter First Number : 45
Enter Second Number : 23
Enter Third Number : 09
Smallest among 45, 23 and 9 is : 23





Floating Point Values
=============================== 
The above program assumes the three numbers are of integer type,  so now let's look into the comparison of three numbers having floating point values.


As usual, take 3 variables first, second and third,  assuming the first floating point number is the smallest , compare the smallest number with the second and third,  set the  smallest number accordingly after comparison.

Once the if block completes, the smallest variable will contain the smallest among these three numbers.  finally print the the smallest number

first = float(input("Enter First Number : "))
second = float(input("Enter Second Number : "))
third = float(input("Enter Third Number : "))

smallest = first
if second < smallest:
   smallest = second
if third < smallest:
   smallest = third

print(f"Smallest among {first}, {second} and {third} is : {smallest}")




OUTPUT
==================

Enter First Number : 55.1
Enter Second Number : 55.01
Enter Third Number : 55.05
Smallest among 55.1, 55.01 and 55.05 is : 55.01





Conclusion
===================

The floating point program will also work for integer values,  so if you are not sure about integer or floating point to use better use a floating point program.



Can we really compare Complex values ??



Comment down below  your thoughts on this.




Post a Comment

0 Comments