Find elements

Python program to find smallest of three numbers

Python program to find the smallest of three numbers

In this tutorial, we will discuss a simple concept of the Python program to find the smallest of three numbers

In this post, we will learn how to find the smallest number among three numbers in the Python programming language.

Python program to find smallest

 

Approaches

  • Initially, the program will check if the first number is smaller than the second and third number. If it is true, the printing function will take place and it will be printed
  • If the above is false, then it will check if the second number is smaller than the first and third number. it will be printed
  • If both the above situation becomes false finally the third number will be printed as it is the smaller number.

 

Find the smallest number using if elif statements

Program 1

This program allows the user to enter three numbers and compare to select the smallest number using if elif statements

num1=int(input("Enter the first number: ")) #Take input from user for first number
num2=int(input("Enter the second number: "))#Take input from user for second number
num3=int(input("Enter the third number: ")) #Take input from user for third number

if(num1<=num2 and num1<=num3):   #Compare the first number with the second and third number
  print(num1," is the smallest")

elif(num2<=num1 and num2<=num3):#Compare the second number with the first and third number
  print(num2," is the smallest")

else:
    print(num3," is the smallest")

When the above code is executed, it produces the following results

Enter the first number: 45
Enter the second number: 65
Enter the third number: 78
45 is the smallest

 

Find the smallest number using nested if statements

Program 2

This program allows the user to enter three numbers and compare to select the smallest number using nested if  statements

num1=int(input("Enter the first number: "))
num2=int(input("Enter the second number: "))
num3=int(input("Enter the third number: "))

if(num1<=num2):
      if(num1<=num3):
            Smallest=num1
      else:
          Smallest=num3
else:
      if (num2<=num3):
        Smallest=num2
      else:
        Smallest=num3

        #display the smallest number
print("Smallest number is", Smallest)

When the above code is executed, it produces the following results

Enter the first number: 57
Enter the second number: 23
Enter the third number: 89
Smallest number is 23

 

Similar post

Find the smallest number among three in C language

Find the smallest number among three in C++ language

Find the smallest number among three in Java language

 

Suggested for you

Operator in Python language

If statements in Python language

Nested if statements in Python language

 

 

 

C program:find smallest of three numbers using function
C++ program to find middle of three number using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago