Repdigit Numbers In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Repdigit Numbers In C++

Repdigit Numbers In C++

BLUF: Mastering Repdigit Numbers In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Repdigit Numbers In C++

C++ is renowned for its efficiency. Learn how Repdigit Numbers In C++ enables low-level control and high-performance computing in the tutorial below.

Introduction:

A repdigit number is a unique numerical concept where every digit in the number is identical. For instance, figures such as 111, 22, and 7777 are classified as repdigit numbers due to their uniformity in digit repetition. The term "repdigit" is derived from merging the words "repeated" and "digit" together.

Repdigit numbers are straightforward yet intriguing to manipulate due to their adherence to a distinct pattern. Even individual digits such as 5 or 9 qualify as repdigit numbers, given that they represent a single digit repeated once.

In the realm of programming, particularly in the C++ language, the process of determining whether a number is a repdigit entails isolating its individual digits and confirming that each digit corresponds to the initial digit. This principle proves valuable in mathematical enigmas, number theory explorations, and coding competitions.

Approach 1: Using Simple Approach

Program:

Let's consider an example to demonstrate Regdigit Numbers in C++.

Example

#include <iostream>
using namespace std;
bool isRepdigit(int number) {
    // Get the last digit
    int lastDigit = number % 10;
    // Loop through the digits
    while (number > 0) {
        int currentDigit = number % 10; // Extract last digit
        if (currentDigit != lastDigit) {
            return false; // Not a repdigit
        }
        number /= 10; // Remove last digit
    }
    return true; // All digits are the same
}
int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    if (num < 0) {
        cout << "Negative numbers are not repdigit." << endl;
        return 0;
    }
    if (isRepdigit(num)) {
        cout << num << " is a repdigit number." << endl;
    } else {
        cout << num << " is not a repdigit number." << endl;
    }
    return 0;
}

Output:

Output

Enter a number: 4444 
4444 is a repdigit number.

Explanation:

  • Input the Number The program starts by asking the user to enter a number. The input number is stored in a variable so it can be processed later.
  • Check for Negative Numbers Since repdigit numbers are typically non-negative (positive or zero), the program includes a check to handle negative inputs. If the number is negative, the program prints a message saying that negative numbers are not repdigit numbers and stops further processing.
  • Extract Digits In order to check if all digits in the number are the same, the program extracts the digits one by one. The process involves dividing the number repeatedly: The last digit is obtained using the remainder operator (%). The rest of the number is obtained by dividing it by 10 (this removes the last digit).
  • Store the First Digit The program starts by storing the last digit (obtained using %) as a reference. This is called the "first digit" because it will be compared with all other digits.
  • Compare Each Digit The program enters a loop that continues until all digits have been extracted and checked: It extracts the next digit using the remainder operator (%). It compares the extracted digit with the first digit (stored earlier). If any digit is not the same as the first digit, the program concludes that the number is not a repdigit.
  • All Digits are the Same If the loop completes and all digits match the first digit, the program determines that the number is a repdigit.
  • Display the Result Based on the checks performed in the loop: If all digits are the same, the program prints a message confirming that the number is a repdigit. If any digit differs, it prints a message saying that the number is not a repdigit.
  • How the Loop Works Imagine a number like 4444: The last digit (4) is extracted and stored as the first digit. The loop continues by removing the last digit, leaving 444. The process repeats until the number becomes 0, and every digit extracted is checked against the first digit. If all are 4, the program confirms it's a repdigit.
  • The program starts by asking the user to enter a number.
  • The input number is stored in a variable so it can be processed later.
  • Since repdigit numbers are typically non-negative (positive or zero), the program includes a check to handle negative inputs.
  • If the number is negative, the program prints a message saying that negative numbers are not repdigit numbers and stops further processing.
  • In order to check if all digits in the number are the same, the program extracts the digits one by one.
  • The process involves dividing the number repeatedly:
  • The last digit is obtained using the remainder operator (%).
  • The rest of the number is obtained by dividing it by 10 (this removes the last digit).
  • The program starts by storing the last digit (obtained using %) as a reference. This is called the "first digit" because it will be compared with all other digits.
  • The program enters a loop that continues until all digits have been extracted and checked:
  • It extracts the next digit using the remainder operator (%).
  • It compares the extracted digit with the first digit (stored earlier).
  • If any digit is not the same as the first digit, the program concludes that the number is not a repdigit.
  • If the loop completes and all digits match the first digit, the program determines that the number is a repdigit.
  • Based on the checks performed in the loop:
  • If all digits are the same, the program prints a message confirming that the number is a repdigit.
  • If any digit differs, it prints a message saying that the number is not a repdigit.
  • Imagine a number like 4444:
  • The last digit (4) is extracted and stored as the first digit.
  • The loop continues by removing the last digit, leaving 444.
  • The process repeats until the number becomes 0, and every digit extracted is checked against the first digit.
  • If all are 4, the program confirms it's a repdigit.

Now, let's explore 123:

  • Initially, the program extracts the final digit (3).
  • Upon extracting the subsequent digit (2), since it differs from 3, the program ceases verification and determines that it is not a repdigit.
  • Complexity analysis:

Time Complexity

The program's time complexity is O(d), with d representing the quantity of digits within the provided number.

Why is the time complexity O(d)?

In this scenario, the software extracts and handles each digit of the number individually.

This process occurs within a loop, with each cycle eliminating the last digit (through division by 10) and comparing it to the first digit.

Number of Loops:

  • When dealing with a number containing d digits, the loop iterates precisely d times as each cycle handles a single digit.
  • In the case of 4444 (a 4-digit number), the loop will iterate 4 times.
  • When dealing with 12345 (a 5-digit number), the loop will execute 5 times.

Conclusion:

  • The loop's runtime is proportional to the number of digits in the number, making the time complexity O(d).

Space Complexity

The program's space complexity is O(1), indicating it consumes a consistent quantity of memory irrespective of the input's magnitude.

Why O(1)?

  • The program uses a fixed number of variables:
  • One is to store the input number.
  • One to store the last digit.
  • One to compare the digits.
  • No additional data structures (like arrays or vectors ) are used.

Memory Consumption:

  • The software operates on a single digit sequentially, avoiding the need to store all digits concurrently.
  • Approach 2: Using Strings to Check for Repdigit Numbers

An alternative method to identify a repdigit number involves converting the numerical value into a string and verifying if all the characters (representing digits) in the string are identical. This technique streamlines the comparison task by leveraging the direct access to individual digits as characters within strings.

Advantages of this Approach

  • Simplicity: By using strings, the code becomes easier to read and understand, as digit extraction and comparison are straightforward.
  • Direct Comparison: The entire number is treated as a sequence of characters, eliminating the need for arithmetic operations like modulo (%) or division (/).
  • Program:

Example

#include <iostream>
#include <string>
using namespace std;
bool isRepdigitUsingString(int number) {
    // Handle negative numbers
    if (number < 0) {
        return false;
    }
    // Convert number to string
    string numStr = to_string(number);
    // Compare each character with the first character
    char firstDigit = numStr[0];
    for (char digit : numStr) {
        if (digit != firstDigit) {
            return false; // Not a repdigit
        }
    }
    return true; // All digits are the same
}
int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    if (isRepdigitUsingString(num)) {
        cout << num << " is a repdigit number." << endl;
    } else {
        cout << num << " is not a repdigit number." << endl;
    }
    return 0;
}

Output:

Output

Enter a number: 1234
1234 is not a repdigit number.

Explanation:

  • Handle Negative Numbers The first step in the program is to check if the input number is negative. Since repdigit numbers are typically non-negative, any negative input is immediately identified as not a repdigit. If the number is negative, the program stops further processing.
  • Convert the Number to a String The number is converted into a string. In string form, each digit of the number becomes a separate character. For example, the number 4444 becomes "4444" (a string of four characters). The number 123 becomes "123".
  • Identify the First Digit The first character of the string is stored separately. This character will be used as the reference to compare all other characters in the string.
  • Compare All Characters The program loops through each character in the string: For every character, it checks if it is the same as the first character. If any character is different from the first character, the program concludes that the number is not a repdigit. If all characters are the same, it confirms the number is a repdigit.
  • Output the Result After checking all characters: If all characters match the first character, the program prints that the number is a repdigit. If any character is different, the program prints that the number is not a repdigit.
  • The first step in the program is to check if the input number is negative.
  • Since repdigit numbers are typically non-negative, any negative input is immediately identified as not a repdigit.
  • If the number is negative, the program stops further processing.
  • The number is converted into a string.
  • In string form, each digit of the number becomes a separate character.
  • For example, the number 4444 becomes "4444" (a string of four characters).
  • The number 123 becomes "123".
  • The first character of the string is stored separately.
  • This character will be used as the reference to compare all other characters in the string.
  • The program loops through each character in the string:
  • For every character, it checks if it is the same as the first character.
  • If any character is different from the first character, the program concludes that the number is not a repdigit.
  • If all characters are the same, it confirms the number is a repdigit.
  • After checking all characters:
  • If all characters match the first character, the program prints that the number is a repdigit.
  • If any character is different, the program prints that the number is not a repdigit.
  • Example Walkthrough

Let's examine several instances to grasp the procedure:

Input: 4444

The number is converted to the string "4444".

The initial character ('4') is saved as a point of reference.

Each character in the string is compared to '4':

  • '4' == '4': Match.
  • '4' == '4': Match.
  • '4' == '4': Match.
  • '4' == '4': Match.

As all the characters are identical, the software concludes that 4444 constitutes a repdigit.

Input: 123

The number is converted to the string "123".

The initial character ('1') is saved as a point of reference.

Each character in the string is checked against '1':

  • '1' is equal to '1': Match.
  • '2' is not equal to '1': Mismatch.

Due to the discrepancy, the software determines that 123 does not qualify as a repdigit.

Input: 7

The number is converted to the string "7".

The initial character ('7') is saved as a point of reference.

Given that the string consists of just a single character, it is inherently considered a match.

The program determines that 7 is a repdigit.

Complexity Analysis:

Time Complexity

  • O(d), where d is the number of digits in the input number.
  • The program converts the number into a string, which takes O(d) time.
  • Then, it checks each character in the string, which also takes O(d) time.
  • Overall, the time complexity is proportional to the number of digits in the number.

Space Efficiency

The space complexity is O(d), where d represents the total count of digits in the given input number.

In this scenario, the memory is allocated for holding the string form of the number. As the string encompasses d characters (each representing a digit), the space utilized scales in direct correlation with the digit count.

Properties:

  • All Digits Are Identical A repdigit number is defined as a number where every digit is the same. For instance, 111, 2222, 55555 are all examples of repdigit numbers. The core feature of these numbers is that every digit is identical, which makes them distinct from other numbers. Example: 444 is a repdigit number because all its digits are the same (4), whereas 123 is not because its digits differ.
  • Base Dependent Repdigit numbers are not limited to base 10. They can exist in any numeral system (base-N). For example: In base 2 (binary), 111 is a repdigit, but its decimal equivalent is 7. In base 3, 222 is a repdigit number, but its decimal equivalent is 26. The concept of a repdigit number is thus base-independent, meaning a number can be a repdigit in one base but have a completely different value in another base.
  • A repdigit number is defined as a number where every digit is the same. For instance, 111, 2222, 55555 are all examples of repdigit numbers.
  • The core feature of these numbers is that every digit is identical, which makes them distinct from other numbers.
  • Example: 444 is a repdigit number because all its digits are the same (4), whereas 123 is not because its digits differ.
  • In base 2 (binary), 111 is a repdigit, but its decimal equivalent is 7.
  • In base 3, 222 is a repdigit number, but its decimal equivalent is 26.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience