Table of Contents
Find middle number out of three numbers in Java
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.
Code to discover middle of three numbers
Code to middle of three numbers using if statements
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
Code to middle of three numbers using Nested-if statements
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
Code to middle of three numbers using method
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