This C++ program analyzes a positive integer input as a...

August 27, 2025 at 01:22 AM

#include <iostream> #include <string> using namespace std; int main() { string numero; cout << "Ingrese un número entero positivo: "; cin >> numero; for (char digito = '0'; digito <= '9'; ++digito) { int count = 0; size_t pos = -1; do { pos = numero.find(digito, pos + 1); if (pos != string::npos) count++; } while (pos != string::npos); if (count > 1) { cout << digito << " se repite " << count << " veces" << endl; } } return 0; }

This C++ program analyzes a positive integer input as a string and counts how many times each digit (from '0' to '9') appears in the number. Here's a step-by-step explanation of what the code does:

  1. Include Headers and Declare main:

    • The program includes <iostream> for input/output and <string> for managing the number as a string.
    • The using namespace std; line allows usage of standard library names (like string, cin, and cout) without explicitly prefixing them.
  2. Read a String Input:

    • The program declares a string variable numero to store the input number.
    • It prompts the user to "Ingrese un número entero positivo" (Enter a positive integer).
    • The user inputs a number, which is read as a string using cin >> numero.
  3. Count Digit Frequencies:

    • A for loop iterates over all possible digits ('0' through '9').
    • For each digit (digito), it initializes a counter count to 0 and a variable pos to -1.
    • A do-while loop repeatedly uses numero.find(digito, pos + 1) to locate the positions of the current digit digito in the string.
      • The find function searches for the first occurrence of digito starting after the position pos.
      • If digito is found, pos is updated to the found position, and count is incremented.
      • The loop continues until find returns string::npos, indicating no more occurrences of digito.
  4. Output Duplicated Digits:

    • After the inner loop, if the count of the current digit digito is greater than 1, the program prints a message indicating that the digit appears multiple times.
    • For example, if the user enters 112233:
      1 se repite 2 veces
      2 se repite 2 veces
      3 se repite 2 veces
      
  5. End of Program:

    • The program terminates after processing all digits from '0' to '9'.

Key Features:

  • Handles Numbers as Strings: By treating the input as a string, the code avoids issues with integer types (e.g., very large numbers or leading zeros).
  • Frequency Counting: It uses find and a loop to count occurrences of each digit efficiently.
  • Repeated Digit Reporting: It only outputs digits that appear more than once in the input.

Limitations:

  • The program assumes the input is a valid non-negative integer. If the user enters something invalid (like letters or negative signs), the behavior may be incorrect or undefined.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node