×

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 String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

Second Smallest Number in an Array in Java

By sorting the arrays and returning the second element, we can use Java to discover the second-smallest number in the array. Input:  arr[] = {10, 11, 13, 15, 34, 51} Output: The...

6 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Integer max() method

The max() method of Integer class returns the greater of two int values. It returns the same result as by calling Math.max. Syntax public static int max(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.