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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

20 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago