Open In App

Repdigit Numbers

Last Updated : 23 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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…. 
 

 

Check if N is an Repdigit number

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++




// C++ implementation to check
// if a number is Repdigit
 
#include <iostream>
using namespace std;
 
// Function to check if a number
// is a Repdigit number
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) {
        int digit = num % b;
        num /= b;
        if (prev != -1 && digit != prev)
            return false;
        prev = digit;
    }
 
    return true;
}
 
// Driver code
int main()
{
    int num = 2000, base = 7;
    isRepdigit(num, base) ? cout << "Yes"
                          : cout << "No";
    return 0;
}


Java




// Java implementation to check
// if a number is Repdigit
class GFG{
 
// Function to check if a number
// is a Repdigit number
static 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 code
public 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




# Python3 implementation to check
# if a number is Repdigit
 
# Function to check if a number
# is a Repdigit number
def 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 code
num = 2000
base = 7
if(isRepdigit(num, base)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Vishal Maurya.


C#




// C# implementation to check
// if a number is Repdigit
using System;
class GFG{
 
// Function to check if a number
// is a Repdigit number
static 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 code
public 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


Javascript




<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>


Output: 

Yes

 

Time Complexity: O(n) 
Reference: http://www.numbersaplenty.com/set/repdigit/
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads