C program to find middle among three numbers
- Home
- Find elements
- C program to find middle among three numbers
- On
- By
- 5 Comments
- Categories: 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 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
5 Comments
Middle value of three numbers in C Language : Simple Way – KDJ Guru April 8, 2020 at 7:31 am
[…] you have search this question in google, surely you have found lot of methods that very hard to understand. But in here I’m giving you a very simple and basic method to […]
Middle value of three numbers in C Language : Simple Way – Digital Chalkie April 20, 2020 at 12:44 pm
[…] you have search this question in google, surely you have found lot of methods that very hard to understand. But in here I’m giving you a very simple and basic method to […]
Middle value of three numbers in C Language : Simple Way – KDJ Guru December 15, 2020 at 5:19 pm
[…] you have search this question in google, surely you have found lot of methods that very hard to understand. But in here I’m giving you a very simple and basic method to […]
samir March 3, 2024 at 8:49 am
wow
samir March 3, 2024 at 8:50 am
wow so good i become gay