# Middle of three numbers using minimum comparisons in C++

This tutorial helps you to learn how to find the middle of three numbers using minimum comparison in C++.

The program mainly consists of 2 parts

1. To compare and arrange the number in descending order.
2. Then find the middle and return the number.

#### Algorithm:

The main use of this code is when we are writing a long program there may be a chance of committing an error, in this program the error finding is easy. The main algorithm in this code is comparing each number and arrange them in descending order. Then find the middle number Take 3 numbers a=90, b=21, c=56 then take the a-b, b-c, c-a differences number and store them in variables. Using function makes the code modular and easy to get the error, any further change can be easily done. Their differences are multiplied and checked whether positive or negative. The main aim of the algorithm is to make code efficient. Then the numbers are arranged in descending order and the middle number is returned. To reduce the compilation time and to get the desired output.

```#include <iostream>
using namespace std;

int midOfThree(int a, int b, int c)
{
int m = a - b;
int p = b - c;
int k = a - c;

if (m * p > 0)
return b;
else if (m * k > 0)
return c;
else
return a;
}
int main()
{
int a,b,c;
cout<<"Enter 3 numbers:";
cin>>a>>b>>c;
cout << middleOfThree(a, b, c);
return 0;
}```
```input:90,21,56
output:56```

Also read: How to calculate moving average in C++