Table of Contents
C++ code to find middle numbers out of three numbers
In this article, we will discuss the concept of C++ 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 C++ program.
Code 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 declaration and initialization in C++ language
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num1=1234,num2=9876,num3=4563; //variable declaration and initialization 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 result
4563 is a middle number
Program to find middle numbers out of three using user input
In this code, we will find middle number out of three numbers using get input from the user in C++ language
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2,num3;//variable declaration cout<<"Enter three numbers for find middle\n"; //ask input from the user cin>>num1>>num2>>num3; //store the input numbers in the num1,num2 and num3 variables 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 result
Enter three numbers in the variable middle 234 890 543 543 is a middle number
Program to find middle numbers out of three using function
In this code, we will find middle number out of three numbers using user defined function in C++ language
Program 3
#include <iostream> #include <conio.h> using namespace std; int middleNumber(int,int,int);//function prototype int main() { int num1=6782,num2=1543,num3=8976;//variable declaration //Store the input in the variable num1,num2 and num3 cout<<"Middle number is "<<middleNumber(num1,num2,num3); //Call the function and display result on the screen getch(); return 0; } int middleNumber(int num1,int num2,int num3){//function definition 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 result
Middle number is 6782
Find middle numbers out of three numbers using function with user input
In this program, we will find middle number out of three numbers using function with get input from the user in C++ language
Program 4
#include <iostream> #include <conio.h> using namespace std; int middleNumber(int,int,int);//function prototype int main() { int num1,num2,num3;//variable declaration cout<<"Enter the three numbers\n"; //Ask input from the user cin>>num1>>num2>>num3; //Store the input in the variable num1,num2 and num3 cout<<"Middle number is "<<middleNumber(num1,num2,num3); //Call the function and display result on the screen getch(); return 0; } int middleNumber(int num1,int num2,int num3){//function definition 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 result
Enter the three numbers 456 987 512 middle number is 512
Suggested post
Nested if statements in C++ language
Similar post
Program to find middle numbers out of three numbers in C++
Program to find middle numbers out of three numbers in C
Program to find middle numbers out of three numbers in java
Program to find middle numbers out of three numbers in Python