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.

Program to find the middle of three numbers

 

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

Operator in C++

C++ Datatypes

If statements in C++

 

Java program to find middle of three numbers
C program to find middle among three numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago