Find elements

Java program to find middle of three numbers

Java program to find middle of three numbers

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 to find the middle of three numbers

 

Find the middle number of three integer numbers

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

 

Find the middle number of three float numbers

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

C Program to largest and smallest among three numbers
Cpp program to find middle of three numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago