Javatpoint Logo
Javatpoint Logo

Repdigit Numbers in Java

In this section, we will learn what is a repdigit numbers and also create Java programs to check if the given number is a repdigit number or not. The repdigit number program is frequently asked in Java coding interviews and academics.

Repdigit Number

Repdigit is short for repeated digits. It is an OEIS sequence A010785. A repdigit is an integer N that has the same digits in its representation in base B and they are equal.

The repdigits composed by repeated ones are called rep-units.

Repdigit Number Example

Suppose, 444 is a number and base are 10 then the number 444 will be a repdigit number. Similarly, if 2000 is a number and given base is 7 then it will be a repdigit number. Because in base 7, the number 2000 can be written as 5555.

First few repdigit numbers are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 444, 555, 666, 777, 888, 999, 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999, 111111, 222222, 333333, 444444, 555555, 666666, etc.

Steps to Find Repdigit Number

  • One by one find all the digits of N in base B.
  • Compare every digit with its previous digit.
  • If any digit is not equal to the previous digit then return false.
  • Otherwise return true.

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

Repdigit Number Java Program

In the following program, we have entered a number N and check it is a repdigit number in base (B) or not.

RepdigitNumberExample.java

Output 1:

Enter the number: 2000
Enter the base: 7
It is a repdigit number.

Output 2:

Enter the number: 5555
Enter the base: 2
It is not a repdigit number.

Output 3:

Enter the number: 66666
Enter the base: 10
It is a repdigit number.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA