Autobiographical Number In C++ - C++ Programming Tutorial
C++ Course / Data Structures / Autobiographical Number In C++

Autobiographical Number In C++

BLUF: Mastering Autobiographical Number 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: Autobiographical Number In C++

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

An autobiographical numeral (n) is an integer with b digits in a particular base. In such a numeral, the digit at index p (with the first digit being at index 0 and the last digit at index (b−1) signifies the frequency of the digit p in the numeral.

Example:

10020100

To represent a number in base 4 with an autobiographical twist, every digit at a specific position (starting from the leftmost digit at position 0) needs to indicate how many times that digit occurs in the number.

Step-by-step verification of 10020100:

  • First digit (position 0): The digit is 1, indicating that there should be one zero in the number.The number 10020100 contains precisely one zero, hence this requirement is satisfied.
  • Second digit (position 1): The digit is 0, which indicates that there should be no 1s in the number. The number 10020100 has no 1s, hence this criterion is met.
  • Third digit (position 2): The third digit (position 2) is 0, which indicates that the number should contain no 2s. The number 10020100 contains two 2's, hence the condition is not satisfied.
  • Approach:

  • Determine the Range: Use the formula [10^(n− 1),10^n− 1] to calculate the range of an N-digit number. Each number inside this range is examined to determine whether it is autobiographical.
  • Convert Number to String: Converting a number to a string provides simple access to each digit and position.
  • Iterate Through Each Digit: Use an outer loop to iterate through each digit of the string, storing the current value in a variable as the expected count.
  • Count Digit Occurrences: To count digit occurrences, use an inner loop that compares the current outer loop index to each digit in the string. If they match, increase the number of times that digit appears. If they match, increase the number of times that digit appears.
  • Check Autobiographical Condition: To check the Autobiographical Condition, compare the actual occurrence count of the digit (from the inner loop) to the expected count in the variable. If all digits meet this requirement, the number is autobiographical.
  • Repeat for All Numbers in Range: Check each number and print whether it meets the autobiographical requirement.
  • Example:

Let's consider an example to demonstrate the concept of Autobiographical Number in C++.

Example

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
// Function to check if a number is autobiographical.
bool isAutobiographical(int num_ber)
{
    // Convert the number to a string for easy digit access.
    string numStr = to_string(num_ber);
    int size = numStr.size();
    // To verify the autobiographical conditions, loop through each digit.
    for (int i = 0; i < size; i++) 
    {
        // Get the expected count of digit 'i' from the current position.
        int expected_Count = numStr[i] - '0';
        // Count the actual occurrences of digit 'i'.
        int actual_Count = 0;
        for (char c : numStr)
        {
            if (c - '0' == i)
            {
                actual_Count++;
            }
        }
        // If the actual count does not equal the expected count, return false.
        if (actual_Count != expected_Count)
        {
            return false;
        }
    }
    return true;
}
// Function to find all autobiographical numbers with the given length.
void findAutobiographicalNumbers(int num) 
{
    // Handling invalid input for number length.
    if (num <= 0) 
    {
        cout << "Length of the number must be greater than 0.\n";
        return;
    }
    // Defining the range of n-digit numbers.
    int lowerLimit = pow(10, num - 1); 
    // Smallest n-digit number
    int upperLimit = pow(10, num) - 1;
    // Largest n-digit number
    bool found = false;
    cout << "Autobiographical numbers with " << num << " digits:\n";
    // Iterating through the range and check each number.
    for (int i = lowerLimit; i <= upperLimit; i++)
    {
        if (isAutobiographical(i))
        {
            found = true;
            cout << i << " ";
        }
    }
    // If no autobiographical numbers were found, display a message.
    if (!found) 
    {
        cout << "None found.\n";
    } 
    else
    {
        cout << endl;
    }
}
int main()
{
    int num;
    // Taking an input from the user.
    cout << "Enter the length of the number: ";
    cin >> num;
    // Calling the function to find the autobiographical numbers.
    findAutobiographicalNumbers(num);
    return 0;
}

Output:

Output

Enter the length of the number: 7
Autobiographical numbers with 7 digits:
3211000

Explanation:

The program is a C++ implementation designed to identify autobiographical numbers with a specified digit count. Autobiographical numbers are those where each digit's position 'i' signifies the quantity of that particular digit in the number itself. Validation of this unique property is carried out by the isAutobiographical function, which calculates and cross-checks the digit counts. Meanwhile, the findAutobiographicalNumbers function is responsible for generating numbers within the specified range based on the provided digit length. It rigorously verifies each number using the isAutobiographical function and showcases all valid outcomes. Additionally, the program gracefully manages user input by allowing users to specify the desired digit length.

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