C Language

Program for find middle number out of three in C

Program for find middle numbers out of three in C

In this article, we will discuss the concept of Program for find middle numbers out of three numbers in C

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  using if statements

In this code, we will find middle number out of three numbers using if statements in C language

Program 1

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

int main()
{
    int num1,num2,num3;//variable declaration
    printf("Enter three numbers\n");
//Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);
//store the entered value on the variables
    //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 result

Enter the three numbers
345
125
566
345 is a middle number

 

Code to find middle numbers  using Nested if statements

In this code, we will find middle number out of three numbers using Nested if in C language

Program 2

#include <stdio.h>
#include <stdlib.h>
int middleNumber(int,int,int);
int main(){
    int num1,num2,num3,middle;//Variable declaration
    printf("Enter the three numbers\n");
//Ask input from the user
    scanf("%d %d %d",&num1,&num2,&num3);//takes input from user


    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;
    }


}
 printf("Middle number is %d",middle);
//Display result on the screen
getch();
    return 0;

}



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

Enter the three numbers:
5678
1234
3456
Middle number is 3456

 

 

Code to find middle numbers  using function

In this code, we will find middle number out of three numbers using function in C language

Program 3

#include <stdio.h>
#include <stdlib.h>
int middleNumber(int,int,int);//function prototype
int main()
{
    int num1,num2,num3;//Variable declaration
    printf("Enter the three numbers\n");
    scanf("%d %d %d",&num1,&num2,&num3);//takes input from user
    printf("Middle number is %d",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
678
123
456
Middle number is 456

 

Suggested post

Operator in C language

if statements in C language

function in C language

 

C++ code to find middle numbers out of three numbers

C code to find middle numbers out of three numbers

Java code to find middle numbers out of three numbers

Python code to find middle numbers out of three numbers

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.