Javatpoint Logo
Javatpoint Logo

Middle Digit Number in Java

In this section, we will learn what is middle digit number and also create Java programs to find the middle digit number. It is frequently asked in Java coding tests and academics.

Middle Digit Number

The middle digit of a number is that which is exactly in-mid of the given number N is called middle digit number. If a number has two middle digits then print both the digits as the middle number.

Middle Digit Number Example

Consider a number 86596. In the given number, 5 is exactly in-mid because there are two digits are in the left and the same number of digits are in the right of 5.

Let's take another number 178903. In the given number, if we make pair of three-three digits, there will no digits remain in-mid. If we make pair of two-two digits, we will get two digits in-mid. The case is a little bit confusing.

In such a scenario, the middle digit number has two digits. In our case, the mid-digits are 89. Sometimes, we consider only the first digit as the middle digit number. Usually, we consider both digits as a middle digit number.

Steps to Find Middle Digit Number

1. Read or initialize a number N.

2. Calculate the length of the given number N by using the following formula:

Length of N = log10(N) + 1

3. Find the first half of the given number by using the following formula:

Middle Digit Number in Java

4. In order to compute the middle digit number of N, use the following formula:

The last digit of the first half of N = (the first half of N) % 10 = 1

Let's implement the above logic in a Java program.

Middle Digit Number Java Program

MiddleDidgitNumberExample1.java

Output:

The middle digit number is: 7

MiddleDigitNumberExample2.java

Output 1:

Enter the number: 123456
Middle number: 34

Output 2:

Enter the number: 45763
Middle number: 7





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA