×

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up of repeated occurrences of the same digit (often implicitly decimal). These are a few examples, 99, 555, 1111, and 88888. Palindromic and multiples of repunits and repdigits are all numbers. In particular, the Mersenne primes and the repunit primes are two other well-known types of repdigits (which are repdigits when represented in the binary).

Repdigits are a representation of the number a B b 1 B 1 in base Y display style Y, where 0 x Y display style 0 x Y is the replicated digit and 1 y display style 1 y is the count of repetitions. For instance, the base 10 representation of the repdigit 99999 is 9 10 5 1 10 1 display style 7times frac 105-110-1.

Brazilian numbers are a subset of repdigits that can be represented as a repdigit in various bases but exclude the repdigit 11 and mono values (or all the numbers will be Brazilian). For instance, the repdigit 33 in base 8 makes the number 27 a Brazilian number. In contrast, the repdigit 118, which is not permitted under the description of Brazilian numbers, makes the number 9 not a Brazilian number.

Creating Java programmes and learning about repdigit numbers are covered in this section.

To determine whether the provided value is a repdigit value or not. The repdigit number program is widely tested in academic settings and Java code interviews.

The Repdigit number

Repeated digits are abbreviated as repdigit. The sequence is A010785 from the OEIS. The repdigit is a numeric N that is equal and has the same number of digits inside its expression in base B.

Rep-units are the repdigits made up of repeated ones.

Example for the repdigits numbers

If the number 777 has a base of 10, then it is a repdigit number. In a similar way, 2000 will become a repdigit number if the specified base is 7 and the number in question. Because 2000 can be expressed as 5555 in base 7.

The initial repdigit numbers are as follows:

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 etc. can be possibly considered as the repdigit numbers.

Finding a Repdigit Number: Steps

  • Find each of the N digits in base B one by one.
  • Every digit should be compared to its predecessor.
  • Return false whether any value is not comparable to the previous digit.
  • If not, return true.

Let's put the actions mentioned above into a Java program.

Java Program for Repdigit Number

We entered the number N into the software below to see whether it was a repdigit value is base (B) or not.

Let the file name of the program is RepdigitNumberDemo.java

import java.util.Scanner; 
public class RepdigitNumberDemo 
{ 
// function to determine whether or if a particular value is indeed a Repdigit number
static boolean theRepdigitNumber(int number, int s) 
{ 
// given the starting value of -1 since it is the lowest possible digit, variable saves the preceding digit.
int previous = -1; 
// from right to left, traverse the specified number in a loop.
while (number != 0) 
{ 
// determines the given number's last digit
int lastdigit = number % s; 
// divides by base the given number (s)
number = number/s; 
// if previous is not -1, then execute
if (previous != -1 && lastdigit != previous) 
return false; 
previous = lastdigit; 
} 
return true; 
} 
//It is the driver code 
public static void main(String args[]) 
{ 
int number, s;
Scanner sc=new Scanner(System.in); 
System.out.print("Please Enter the number: "); 
number = sc.nextInt();
System.out.print("Please Enter the base: "); 
s = sc.nextInt(); 
if(theRepdigitNumber(number, s)) 
{ 
System.out.print("This number is a repdigit."); 
} 
else 
{ 
System.out.print("It is not a number with repdigits."); 
} 
} 
} 

Output 1:

Please enter the number: 2000
Please Enter the base: 7
This number is a repdigit.

Output 2:

Please enter the number: 5555
Please Enter the base: 2
It is not a number with repdigits.

Output 3:

Please enter the number: 66666
Please Enter the base: 10
This number is a repdigit.

Related Topics

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Blocking Queue in Java Example

Let's first briefly comprehend queue before moving on to the topic of "Blocking Queue." A queue seems to be an orderly list of items in which elements are added from...

8 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Java.net.SocketException

Exception The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle...

4 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.