Home » Python Program to find the Biggest and Smallest of 3 numbers

Python Program to find the Biggest and Smallest of 3 numbers

Python Program to find the Biggest and Smallest of 3 numbers

Hello people, welcome to Programming In Python! Here I will discuss a simple Python program that finds the biggest and smallest of 3 numbers. Here we use the concept of functions in this program.

We use two functions biggest() and smallest() to find the biggest number and smallest number respectively and finally return the result.

https://www.youtube.com/watch?v=Cz6q5u4iW5w”]

You can watch the video on YouTube here

Program on GitHub

Biggest and Smallest – Code Visualization

Task :

To find the smallest and biggest number out of the given 3 numbers.

Approach :

  • 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 fail, num3 is the 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 fail, num3 is smaller
    • Print the smallest number

Program on GitHub

Ad:
The Complete Python Bootcamp From Zero to Hero in Python – Enroll Now.
Udemy

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)

 

Output :

Biggest and Smallest of 3 numbers
Biggest and Smallest numbers

Biggest and Smallest of 3 numbers
Biggest and Smallest numbers

Program on GitHub

Course Suggestion

Want to learn Python with strong fundamentals? If yes, I strongly suggest you take the course below.
Course: Fundamentals of Programming in Python

 

Online Python Compiler

Leave a Reply

Your email address will not be published. Required fields are marked *