Find elements

Python program to middle among three numbers

Python program to find middle among three numbers

In this tutorial, we will discuss the Python program to find middle among three numbers.

This post explains how to find middle number among three numbers using if statements with the operator in Python.

There are many ways to find the middle number of three numbers. but here, we mainly focus on using if statements or if else-if statements and with or without the and operator.

Here, we provide four programs to find the middle number among the three numbers.

first two programs guide to find the middle number among three integer numbers.

Second two programs guide to find the middle number among three floating point numbers in different two methods.

Program to find the middle of three numbers

 

Find the middle number of three integer numbers

program 1

Find the middle number using if elif statements without and operator

#takes input from user for num1,num2,num3
num1=int(input("Input first number: "))
num2=int(input("Input second number: "))
num3=int(input("Input tird number: "))
if num1>num2:
    if num1<num3:
        median= num1
    elif num2>num3:
        median= num2
    else:
        median= num3
else:
    if num1>num3:
        median= num1
    elif num2<num3:
        median= num2
    else:
        median= num3
print("The middle number is",median)

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

Input first number: 35
Input second number: 98
Input tird number: 65
The middle number is 65

 

Program 2

Find the middle number using if statements with and operator

#takes input from user for num1,num2,num3
num1=int(input("Input first number: "))
num2=int(input("Input second number: "))
num3=int(input("Input tird number: "))
 //checking num1 is a middle number or not
if(num2>num1 and num1>num3 or num3>num1 and num1>num2):
    print(num1, "is a middle number")
 //checking num2 is a middle number or not
if(num1>num2 and num2>num3 or num3>num2 and num2>num1):
    print(num2, "is a middle number")

 //checking num3 is a middle number or not
if(num1>num3 and num3>num2 or num2>num3 and num3>num1):
    print(num3, "is a middle number")

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

Input first number: 45
Input second number: 89
Input tird number: 34
45 is a middle number

 

Find the middle number of three float numbers

program 3

Find the middle number using if elif statements without and operator

num1=float(input("Input first number: "))
num2=float(input("Input second number: "))
num3=float(input("Input tird number: "))
if num1>num2:
    if num1<num3:
        median= num1
    elif num2>num3:
        median= num2
    else:
        median= num3
else:
    if num1>num3:
        median= num1
    elif num2<num3:
        median= num2
    else:
        median= num3
print("The middle number is",median)

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

Input first number: 87.67
Input second number: 12.3
Input tird number: 56.45
The middle number is 56.45

 

Program 4

Find the middle number using if statements with and operator

num1=float(input("Input first number: "))
num2=float(input("Input second number: "))
num3=float(input("Input tird number: "))
 //checking num1 is a middle number or not
if(num2>num1 and num1>num3 or num3>num1 and num1>num2):
    print(num1, "is a middle number")
 //checking num2 is a middle number or not
if(num1>num2 and num2>num3 or num3>num2 and num2>num1):
    print(num2, "is a middle number")
 //checking num3 is a middle number or not
if(num1>num3 and num3>num2 or num2>num3 and num3>num1):
    print(num3, "is a middle number")

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

Input first number: 23.43
Input second number: 89.09
Input tird number: 45.32
45.32 is a middle number

 

Suggested for you

Operator in Python

if statements in python

 

C program to find middle among three numbers
Java code to largest and second largest elements in array
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