Python program to middle among three numbers
- Home
- Find elements
- Python program to middle among three numbers
- On
- By
- 1 Comment
- Categories: 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.
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
if statements in python
1 Comment
Jyoti September 26, 2020 at 2:03 am
Notify me when new updates will come