Find elements

Java program to find middle of three number using method

Java program to find middle of three number using method

In this tutorial , we will discuss the concept of Java program to find middle of three number using method

This post describes how to find the middle number among three numbers using if statements in Java language.

There are many ways to find the middle number of the three numbers. but here, we mainly focus on using if-else-if statements in Java.

Find middle of three number

Using Nested if statements to find the middle number of three numbers

import java.util.Scanner;
class FindMiddleNum{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter three numbers: ");
int num1=scan.nextInt();//reads num1 from user
int num2=scan.nextInt();//reads num2 from user
int num3=scan.nextInt();//reads num3 from user

System.out.print("Middle number is "+middleNumber(num1,num2,num3));//calling the method

}
static int middleNumber(int num1,int num2,int num3){//method definition
    int middle=0;
    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;
}
}

 

When the above code is executed, it produces the following results

Enter the three numbers
23
45
78
Middle number is 45

Similar post

C program to find the middle of three number using the function

C++ program to find the middle of three number using the function

Python program to find the middle of three number using the function

Java code to find middle of three numbers

C program to find middle of three numbers

C++ program to find middle of three numbers

Python program to find middle of three numbers

Java code to find largest of three numbers in an array

Java code to find smallest of three numbers in an array

 

Suggested for you

The operator in Java language

If statement in Java language

Data types and variables in Java language

Variable in Java

 

 

 

 

Python program to find middle of three number using function
Calculate power of a number using recursion in C language
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…

2 months 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…

2 months ago