If statement

Python code to find middle numbers out of three

Python code to find middle numbers out of three numbers

In this article, we will discuss the concept of Python code to find middle numbers out of three numbers

In this post, we are going to learn how to write a program to find middle number out of three numbers using different methods in Python program.

Program to find middle numbers

Code to find middle numbers  out of three using variable initialization

In this code, we will find middle number out of three numbers using variable  initialization in Python language

Program 1

#Python program to find middle number out of three number
num1=9821;
num2=3456
num3=1230
#Variable declaration and initilaization

if(num2>num1 and num1>num3 or num3>num1 and num1>num2):
    print(num1, "is a middle number")
    #check and display whether the num1 is middle

if(num1>num2 and num2>num3 or num3>num2 and num2>num1):
    print(num2, "is a middle number")
    #check and display whether the num2 is middle

if(num1>num3 and num3>num2 or num2>num3 and num3>num1):
    print(num3, "is a middle number")
    #check and display whether the num3 is middle

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

3456 is a middle number

 

 

Code to find middle numbers  out of three using user input

In this code, we will find middle number out of three numbers using input from the user in Python language

Program 2

num1=int(input("Input first number: "))
#Takes input from user for num1
num2=int(input("Input second number: "))
#Takes input from user for num2
num3=int(input("Input tird number: "))
#Takes input from user for num3

if(num2>num1 and num1>num3 or num3>num1 and num1>num2):
    print(num1, "is a middle number")
    #check and display whether the num1 is middle

if(num1>num2 and num2>num3 or num3>num2 and num2>num1):
    print(num2, "is a middle number")
    #check and display whether the num2 is middle

if(num1>num3 and num3>num2 or num2>num3 and num3>num1):
    print(num3, "is a middle number")
    #check and display whether the num3 is middle

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

Input first number: 132
Input second number: 768
Input tird number: 543
543 is a middle number

 

Code to find middle numbers  out of three using function

In this Program, we will find middle number out of three numbers using function in Python language

Program 3

#Python program to find middle number among three
num1=5432
num2=9876
num3=6745

def middleNum():#function definition
    middle=0;
    if(num1>num2):
        if(num2>num3):
           middle=num2;
    
        elif(num3>num1):
           middle=num1;
    
        else:
            middle=num3;

    else:

        if(num2<num3):
            middle=num2;
    
        elif(num3<num1):
            middle=num1;
    
        else:
            middle=num3;
    
    print("middle number is",middle);
middleNum();#Calling the function

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

middle number is 6745

 

 

 

Code to find middle numbers  out of three using function with user input

In this Program, we will find middle number out of three numbers using function with user input in Python language

Program 4

#Python program to find middle number among three
num1=int(input("Enter the first number: "))
#get input from the user for first number
num2=int(input("Enter the second number: "))
#get input from the user for second number
num3=int(input("Enter the third number: "))
#get input from the user for third number

def middleNum():#function definition
    middle=0;
    if(num1>num2):
        if(num2>num3):
           middle=num2;
    
        elif(num3>num1):
           middle=num1;
    
        else:
            middle=num3;

    else:

        if(num2<num3):
            middle=num2;
    
        elif(num3<num1):
            middle=num1;
    
        else:
            middle=num3;
    
    print("middle number is",middle);
middleNum();#Calling the function

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

Enter the first number: 432
Enter the second number: 321
Enter the third number: 654
middle number is 432

 

Suggested for you

Operator in Python Language

if elif in Python language

Nested if in Python Language

Data type in Python language

 

Similar post

C++ code to find middle numbers out of three numbers

C code to find middle numbers out of three numbers

Java code to find middle numbers out of three numbers

Python code to find middle numbers out of three numbers

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.