Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program for find middle number out of three in C

Find middle number out of three numbers in Java

Posted on March 13, 2021March 13, 2021

Table of Contents

  • Find middle number out of three numbers in Java
    • Code to discover middle of three numbers
      • Code to middle of three numbers  using if statements
      • Code to  middle of three numbers  using Nested-if statements
      • Code to  middle of three numbers  using method

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

Operator 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

 

 

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes