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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago