Codeforcoding

C++ program to find middle of three number using function

C++ program to find middle of three number using function

In this tutorial, we will discuss the concept of C++ program to find middle among three numbers using function

This post describes how to find the middle number among three numbers using if statements in C++ language.

There are many ways to find the middle number of the three numbers. but here, we mainly focus if-else-if statements to find the middle number.

 

C++ program to find middle
Find middle number

 

Using Nested if statements  to find the middle

Program

#include <iostream>
#include <conio.h>
using namespace std;

int middleNumber(int,int,int);
int main()
{
    int num1,num2,num3;
    cout<<"Enter the three numbers\n";
    cin>>num1>>num2>>num3;//takes input from user
    cout<<"Middle number is " <<middleNumber(num1,num2,num3);
    getch();
    return 0;
}

int middleNumber(int num1,int num2,int num3){
    int middle=0;
    if(num1>num2){
        if(num2>num3){
        middle=num2;
    }
    else if(num3>num1){
        middle=num1;
    }
    else{
        middle=num3;
    }
    }

    else{

        if(num2<num3){
        middle=num2;
    }
    else if(num3<num1){
        middle=num1;
    }
    else{
        middle=num3;
    }

}

}

 

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

Enter the three numbers
45
56
78
Middle number is 45

Similar post

Java program to find the middle of three number using Method

C code to find the middle of three number using the function

C++ code to find the middle of three number using the function

Python code to find the middle of three number using the function

Java code to find middle of three numbers

C code to find middle of three numbers

C++ code to find middle of three numbers

Python code to find middle of three numbers

Java code to find largest of three numbers in an array

Java code to find smallest of three numbers in an array

C code to find the middle of three number using the function

C++ code to find the middle of three number using the function

 

Suggested for you

The operator in C++ language

If statement in C++ language

Nested if statement in C++ language

Data type in C++ language

Variable in C++ language

 

Operator in C language

If statement in C language

Data type in C language

Variable in C language

 

The operator in Python language

If statement in Python language

function in Python

Data types and Variable in Python

 

 

 

 

 

Python program to find smallest of three numbers
C program to find middle among three number using function
Exit mobile version