Table of Contents
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
C++ code to find middle numbers out of three numbers
C code to find middle numbers out of three numbers