Find elements

C program to find middle among three numbers

C program to find middle among three numbers

In this tutorial, we will discuss the C program to find middle among three numbers.

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

There are many ways to find the middle number of the 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 programs focus to find the middle number of three integer numbers.

Second two program focus to find the middle number of three floating point numbers in different two methods.

Find a middle of three numbers

Find the middle number of three integer numbers

Using if statements and with & operator to find the middle

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,num3;
    printf("Enter three numbers\n");
    scanf("%d %d %d",&num1,&num2,&num3);
//takes input from user
    //checking num1 is a middle number or not
    if(num2>num1 && num1>num3 || num3>num1 && num1>num2){
        printf("\n%d is a middle number",num1);
    }

    //checking num2 is a middle number or not
    if(num1>num2 && num2>num3 || num3>num2 && num2>num1){
        printf("\n%d is a middle number",num2);
    }

    //checking num3 is a middle number or not
    if(num1>num3 && num3>num2 || num2>num3 && num3>num1){
        printf("\n%d is a middle number",num3);
    }
    getch();
    return 0;
}

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

Enter three numbers
23
76
54

54 is a middle number

 

Program 2

Using if-else-if statements and without  the & operator to find the middle

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,num3;//declare variaables
    printf("Enter three numbers to find middle\n");
    scanf("%d %d %d",&num1,&num2,&num3);
//takes input from user for numbers

    if(num1>num2){
        if(num2>num3){
        printf("%d is a middle number",num2);
    }
    else if(num3>num1){
        printf("%d is a middle number",num1);
    }
    else{
        printf("%d is a middle number",num3);
    }
    }

    else{

        if(num2<num3){
        printf("%d is a middle number",num2);
    }
    else if(num3<num1){
        printf("%d is a middle number",num1);
    }
    else{
        printf("%d is a middle number",num3);
    }

    }

    getch();
    return 0;
}

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

Enter three numbers to find middle
23
67
43
43 is a middle number

 

Find the middle number of three floating point numbers

Program 3

Using if statements and with & operator to find the middle

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float num1,num2,num3;
    printf("Enter three numbers\n");
    scanf("%f %f %f",&num1,&num2,&num3);
//takes input from user
    //checking num1 is a middle number or not
    if(num2>num1 && num1>num3 || num3>num1 && num1>num2){
        printf("\n%.2f is a middle number",num1);
    }

    //checking num2 is a middle number or not
    if(num1>num2 && num1>num3 || num3>num2 && num2>num1){
        printf("\n%.2f is a middle number",num2);
    }

    //checking num3 is a middle number or not
    if(num1>num3 && num3>num2 || num2>num3 && num3>num1){
        printf("\n%.2f is a middle number",num3);
    }
    getch();
    return 0;
}

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

Enter three numbers
54.32
23.56
76.4

54.32 is a middle numbers

 

Program 4

Using if-else-if statements and without the  & operator to find the middle

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float num1,num2,num3;//declare variables
    printf("Enter three numbers to find middle\n");
    scanf("%f %f %f",&num1,&num2,&num3);
//takes input from user

    if(num1>num2){
        if(num2>num3){
        printf("%.2f is a middle number",num2);
    }
    else if(num3>num1){
        printf("%.2f is a middle number",num1);
    }
    else{
        printf("%.2f is a middle number",num3);
    }
    }

    else{

        if(num2<num3){
        printf("%.2f is a middle number",num2);
    }
    else if(num3<num1){
        printf("%.2f is a middle number",num1);
    }
    else{
        printf("%.2f is a middle number",num3);
    }

    }

    getch();
    return 0;
}

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

Enter three numbers to find middle
23.56
78.32
45.3
45.30 is a middle number

 

Suggested for you

Operator in C language

If statement in C language

Data type in C language

Variable in C language

 

Cpp program to find middle of three numbers
Python program to 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.

View Comments

Recent Posts

PHP Star Triangle pattern program

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

6 months ago

PHP Full Pyramid pattern program

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

6 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…

6 months ago

Python Full Pyramid star pattern program

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

9 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…

1 year 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,…

1 year ago