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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #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
1 2 3 4 5 6 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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
1 2 3 4 5 6 | Input first number: 20 Input second number: 30 Input tird number: 40 30 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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
1 2 3 4 5 6 | Input first number: 12.56 Input second number: 10.74 Input tird number: 13.14 The middle number is 12.56 |
Program 4
Find the middle number using if statements with and operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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
1 2 3 4 5 6 | Input first number: 12 Input second number: 45 Input tird number: 78 45.0 is a middle number |