Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program for find middle number out of three in C

Program for find middle number out of three in C

Posted on March 13, 2021March 13, 2021

Table of Contents

  • Program for find middle numbers out of three in C
    • Code to find middle numbers
      • Code to find middle numbers  using if statements
      • Code to find middle numbers  using Nested if statements
      • Code to find middle numbers  using function

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes