Cpp program to find middle of three numbers
- Home
- Find elements
- Cpp program to find middle of three numbers
- On
- By
- 0 Comment
- Categories: Find elements
Cpp program to find middle of three numbers
Cpp program to find middle of three numbers
In this tutorial, we will discuss the Cpp program to find middle of three numbers.
This post explains how to find middle number among three numbers using if statements and & operator in C++.
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 & operator.
Here, we provide four programs to find the middle number of three numbers.
first two program focus to find a middle number of three integer numbers.
Second two program focus to find a middle number of three floating point numbers in different two methods.
Find the middle number of three integer numbers
Using if statements with the & operator
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2,num3; cout<<"Enter three numbers\n"; cin>>num1>>num2>>num3;//takes input from user for three numbers //checking num1 is a middle number or not if(num2>num1 && num1>num3 || num3>num1 && num1>num2){ cout<<num1<<"is a middle number"; } //checking num2 is a middle number or not if(num1>num2 && num1>num3 || num3>num2 && num2>num1){ cout<<num2<<"is a middle number"; } //checking num3 is a middle number or not if(num1>num3 && num3>num2 || num2>num3 && num3>num1){ cout<<num3<<"is a middle number"; } getch(); return 0; }
When the above code is executed, it produces the following results
Enter three numbers 27 87 45 45 is a middle number
Program 2
Using if else – if statements without & operator
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2,num3;//variable declaration cout<<"Enter three numbers for find middle\n"; cin>>num1>>num2>>num3;//Takes input from user if(num1>num2){ if(num2>num3){ cout<<num2<<" is a middle number"; } else if(num3>num1){ cout<<num1<<" is a middle number"; } else{ cout<<num3<<" is a middle number"; } } else{ if(num2<num3){ cout<<num2<<" is a middle number"; } else if(num3<num1){ cout<<num1<<" is a middle number"; } else{ cout<<num3<<" is a middle number"; } } getch(); return 0; }
When the above code is executed, it produces the following results
Enter three numbers for find middle 45 12 78 45 is a middle number
Find the middle number of three floating point numbers
Using if statements with the & operator
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { float num1,num2,num3; cout<<"Enter three numbers\n"; cin>>num1>>num2>>num3;//takes input from user for num1,num2,num3; //take input numbers from user //checking num1 is a middle number or not if(num2>num1 && num1>num3 || num3>num1 && num1>num2){ cout<<num1<<"is a middle number\n"; } //checking num2 is a middle number or not if(num1>num2 && num1>num3 || num3>num2 && num2>num1){ cout<<num2<<"is a middle number\n"; } //checking num3 is a middle number or not if(num1>num3 && num3>num2 || num2>num3 && num3>num1){ cout<<num3<<"is a middle number"; } getch(); return 0; }
When the above code is executed, it produces the following results
Enter three numbers 45.34 78.65 23.56 45.34 is a middle number
Program 4
Using if else – if statements without & operator
#include <iostream> #include <conio.h> using namespace std; int main() { float num1,num2,num3; cout<<"Enter three numbers for find middle\n"; cin>>num1>>num2>>num3;//takes input from user for numbers num1,num2,num3; if(num1>num2){ if(num2>num3){ cout<<num2<<" is a middle number"; } else if(num3>num1){ cout<<num1<<" is a middle number"; } else{ cout<<num3<<" is a middle number"; } } else{ if(num2<num3){ cout<<num2<<" is a middle number"; } else if(num3<num1){ cout<<num1<<" is a middle number"; } else{ cout<<num3<<" is a middle number"; } } getch(); return 0; }
When the above code is executed, it produces the following results
Enter three numbers for find middle 23.54 78.54 45.6 45.6 is a middle number
Suggested for you
C++ Datatypes