In this tutorial, we will discuss the Java program to find middle of three numbers.
This post explains how to find middle number among three numbers using if statements and & operator in Java.
There are many ways to find the middle number of three numbers. but here, we mainly focus 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 guide to find the middle number of three integer numbers.
Second two programs guide to find the middle number of three floating point numbers in different two methods.
program 1
Find the middle number using if statements with the & operator
import java.util.*; class FindMiddle{ public static void main (String args[]){ int num1,num2,num3;//variable declaration for three numbers //Using scanner class Scanner scan=new Scanner(System.in); System.out.print("Enter the numbers: "); num1=scan.nextInt();//taking input from user for num1 num2=scan.nextInt();//taking input from user for num2 num3=scan.nextInt();//taking input from user for num3 //checking num1 is a middle number or not if(num2>num1 && num1>num3 || num3>num1 && num1>num2){ System.out.print(num1+"is a middle number"); } //checking num2 is a middle number or not if(num1>num2 && num2>num3 || num3>num2 && num2>num1){ System.out.print(num2+"is a middle number"); } //checking num3 is a middle number or not if(num1>num3 && num3>num2 || num2>num3 && num3>num1){ System.out.print(num3+"is a middle number"); } } }
When the above code is executed, it produces the following results
Enter the numbers 56 34 78 56 is a middle
Program 2
Find the middle number using if else-if statements without & operator
import java.util.*; class Middle_Num1{ public static void main (String args[]){ int num1,num2,num3;//variable declaration for three numbers //Using scanner class Scanner scan=new Scanner(System.in); System.out.print("Enter the numbers: "); num1=scan.nextInt();//taking input from user for num1 num2=scan.nextInt();//taking input from user for num2 num3=scan.nextInt();//taking input from user for num3 if(num1>num2){ if(num2>num3){ System.out.println(num2+" is a middle number"); } else if(num3>num1){ System.out.println(num1+" is a middle number"); } else{ System.out.println(num3+" is a middle number"); } } else{ if(num2<num3){ System.out.println(num2+" is a middle number"); } else if(num3<num1){ System.out.println(num1+" is a middle number"); } else{ System.out.println(num3+" is a middle number"); } } } }
When the above code is executed, it produces the following results
Enter the numbers: 67 23 89 67 is a middle number
Program 3
Find the middle number using if statements with the & operator
import java.util.*; class Middle_Num{ public static void main (String args[]){ float num1,num2,num3;//variable declaration for three numbers //Using scanner class Scanner scan=new Scanner(System.in); System.out.print("Enter the numbers: "); num1=scan.nextFloat();//taking input from user for num1 num2=scan.nextFloat();//taking input from user for num2 num3=scan.nextFloat();//taking input from user for num3 //checking num1 is a middle number or not if(num2>num1 && num1>num3 || num3>num1 && num1>num2){ System.out.print(num1+"is a middle number"); } //checking num2 is a middle number or not if(num1>num2 && num2>num3 || num3>num2 && num2>num1){ System.out.print(num2+"is a middle number"); } //checking num3 is a middle number or not if(num1>num3 && num3>num2 || num2>num3 && num3>num1){ System.out.print(num3+"is a middle number"); } } }
When the above code is executed, it produces the following results
Enter three numbers 27.56 87.34 45.23 45.23 is a middle
Program 4
Find the middle number using if else-if statements without & operator
import java.util.*; class Middle_Num1{ public static void main (String args[]){ float num1,num2,num3;//variable declaration for three numbers //Using scanner class Scanner scan=new Scanner(System.in); System.out.print("Enter the numbers: "); num1=scan.nextFloat();//taking input from user for num1 num2=scan.nextFloat();//taking input from user for num2 num3=scan.nextFloat();//taking input from user for num3 if(num1>num2){ if(num2>num3){ System.out.println(num2+" is a middle number"); } else if(num3>num1){ System.out.println(num1+" is a middle number"); } else{ System.out.println(num3+" is a middle number"); } } else{ if(num2<num3){ System.out.println(num2+" is a middle number"); } else if(num3<num1){ System.out.println(num1+" is a middle number"); } else{ System.out.println(num3+" is a middle number"); } } } }
When the above code is executed, it produces the following results
Enter the numbers: 45.34 78.7 23.54 45.34 is a middle number
Suggested for you
Operator in Java
If statements in Java
Data type in Java
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…