Repdigit Numbers - GeeksforGeeks

23 Mar, 2021
Repdigit Number is a number N which has all the digits in its representation in base B equal.Some of the Repdigit number are:  
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55....  
 
Given a number N, the task is to check if N is an Repdigit Number in Base B or not. If N is a Repdigit Number in Base B then print “Yes” else print “No”.Examples:  
Input: N = 2000, B = 7 Output: Yes Explanation: 2000 in base 7 is 5555 which has all digits equal.Input: N = 112, B = 10 Output: No  
 
Approach:  
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.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to check// if a number is Repdigit #include <iostream>using namespace std; // Function to check if a number// is a Repdigit numberbool isRepdigit(int num, int b){    // To store previous digit (Assigning    // initial value which is less than any    // digit)    int prev = -1;     // Traverse all digits from right to    // left and check if any digit is    // smaller than previous.    while (num) {        int digit = num % b;        num /= b;        if (prev != -1 && digit != prev)            return false;        prev = digit;    }     return true;} // Driver codeint main(){    int num = 2000, base = 7;    isRepdigit(num, base) ? cout << "Yes"                          : cout << "No";    return 0;}
// Java implementation to check// if a number is Repdigitclass GFG{ // Function to check if a number// is a Repdigit numberstatic boolean isRepdigit(int num, int b){    // To store previous digit (Assigning    // initial value which is less than any    // digit)    int prev = -1;     // Traverse all digits from right to    // left and check if any digit is    // smaller than previous.    while (num != 0)    {        int digit = num % b;        num /= b;        if (prev != -1 && digit != prev)            return false;        prev = digit;    }    return true;} // Driver codepublic static void main(String args[]){    int num = 2000, base1 = 7;    if(isRepdigit(num, base1))    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by Code_Mech
# Python3 implementation to check# if a number is Repdigit # Function to check if a number# is a Repdigit numberdef isRepdigit(num, b) :         # To store previous digit (Assigning    # initial value which is less than any    # digit)    prev = -1     # Traverse all digits from right to    # left and check if any digit is    # smaller than previous.    while (num) :        digit = num % b        num //= b        if (prev != -1 and digit != prev) :            return False        prev = digit    return True # Driver codenum = 2000base = 7if(isRepdigit(num, base)):    print("Yes")else:    print("No") # This code is contributed by Vishal Maurya.
// C# implementation to check// if a number is Repdigitusing System;class GFG{ // Function to check if a number// is a Repdigit numberstatic bool isRepdigit(int num, int b){    // To store previous digit (Assigning    // initial value which is less than any    // digit)    int prev = -1;     // Traverse all digits from right to    // left and check if any digit is    // smaller than previous.    while (num != 0)    {        int digit = num % b;        num /= b;        if (prev != -1 && digit != prev)            return false;        prev = digit;    }    return true;} // Driver codepublic static void Main(){    int num = 2000, base1 = 7;    if(isRepdigit(num, base1))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by Code_Mech
<script> // Javascript implementation to check// if a number is Repdigit      // Function to check if a number    // is a Repdigit number    function isRepdigit( num ,b) {        // To store previous digit (Assigning        // initial value which is less than any        // digit)        let prev = -1;         // Traverse all digits from right to        // left and check if any digit is        // smaller than previous.        while (num != 0) {            let digit = num % b;            num = parseInt(num/b);            if (prev != -1 && digit != prev)                return false;            prev = digit;        }        return true;    }     // Driver code              let num = 2000, base1 = 7;        if (isRepdigit(num, base1)) {            document.write("Yes");        } else {            document.write("No");        } // This code contributed by gauravrajput1 </script>
Yes
 
Time Complexity: O(n) Reference: http://www.numbersaplenty.com/set/repdigit/ 
vishu2908
Code_Mech
GauravRajput1
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Fizz Buzz Implementation
Program to multiply two matrices
Find pair with maximum GCD in an array
Find Union and Intersection of two unsorted arrays
Count all possible paths from top left to bottom right of a mXn matrix
Count ways to reach the n'th stair