Table of Contents
In this article, we will discuss the concept of Find middle number out of three numbers in Java
In this post, we are going to learn how to write a program to find middle numbers out of three numbers using different methods in Java program.
In this code, we will find middle number out of three numbers using if statements in Java language
Program 1
import java.util.*; class Middle_Num{ 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: "); //ask input from user for numbers num1=scan.nextInt();//Storing input from user for num1 num2=scan.nextInt();//Storing input from user for num2 num3=scan.nextInt();//Storing 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"); }//if the num1 is middle,display num1 as middle //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"); }//if the num2 is middle,display num2 as middle //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"); }//if the num3 is middle,display num3 as middle } }
When the above code is executed, it produces the following result
Enter the numbers: 34 12 45 34 is a middle number
In this code, we will find middle number out of three numbers using Nested if in Java language
Program 2
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 three numbers: "); //taking input from user for numbers num1=scan.nextInt();//storing input from user for num1 num2=scan.nextInt();//storing input from user for num2 num3=scan.nextInt();//storing 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 result
Enter three numbers: 123 456 567 456 is a middle numbers
In this code, we will find middle out of three numbers using method in Java language
Program 3
import java.util.*; class Middle_Num_Method{ 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 three numbers: "); //taking input from user for numbers num1=scan.nextInt();//storing input from user for num1 num2=scan.nextInt();//storing input from user for num2 num3=scan.nextInt();//storing input from user for num3 System.out.print("The middle number is: "+middleNum(num1,num2,num3)); }//Call the method and display output static int middleNum(int num1,int num2, int num3){//method definition int middle; 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; } } return middle;//return middle value to main method } }
When the above code is executed, it produces the following result
Enter three numbers: 543 213 678 The middle number is: 543
Suggested for you
Data type and variable in Java
Class and main method explanation in Java
Variable types in Java language
Similar post
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.