Learning C++ Programming from Scratch, Day 403: 1083 – Palindrome Numbers; Problem Set Answers; Fourth Method

1083 – Palindrome Numbers

Learning C++ Programming from Scratch, Day 403: 1083 - Palindrome Numbers; Problem Set Answers; Fourth Method

This program addresses an interesting mathematical problem: Calculating how many times a number needs to undergo the “reverse and add” operation to become a palindrome (a number that reads the same forwards and backwards, such as 121).

How does the program work?

  1. First, the user inputs a number (for example, 57).

  2. The program performs the following actions:

  • Reverses the number (57→75).

  • Checks if it is a palindrome (57≠75, it is not).

  • If it is not, it adds the original number and the reversed number (57+75=132).

  • Records this as the first operation.

  • Repeats this process for the new number:

    • 132→231.

    • 132≠231, continue adding (132+231=363).

    • Records the second operation.

  • Now 363 is a palindrome (363 reversed is still 363), stop!

  • Finally, informs the user: 2 operations were used.

  • Special Note::

    • The program uses the <span>ji</span> variable to record the number of operations.

    • It will stop when the number becomes 1 (this is a special setting).

    • The final output of <span>ji-1</span> is because the first check does not count as a real operation.

    Reference Code

    #include<iostream>  // Include input-output library, allows using cin/coutusing namespace std; // Use standard namespace to avoid writing std::int main() // Main function of the program, execution starts here{    // Define variables:    int n;    // Store the user input number    int ni;   // Store the result of the reversed number    int ce;   // Temporary variable to hold the number during reversal    int ji=0; // Counter to record the number of operations
        cin>>n; // Read the user input number from the keyboard
        // Main loop: continue while n is not equal to 1 (this is a special setting)    while(n!=1){        ni=0;  // Clear the reverse result before each iteration        ce=n;   // Save the current number to temporary variable ce        ji++;   // Increment operation count
            // Number reversal process (core algorithm)        while(ce!=0){          // Continue while ce still has digits            ni=ni*10+ce%10;    // Add the last digit of ce to the end of ni            ce=ce/10;          // Remove the last digit from ce        }
            // Check if it is a palindrome        if(n!=ni){       // If it is not a palindrome            n=n+ni;      // Add the number and its reversed number        }    }
        cout<<ji-1; // Output the number of operations (subtract 1 because the first check does not count as an operation)    return 0;   // Program ends normally}

    1. What does the program do?

    This program calculates how many times you need to repeatedly “add its reverse” to any given number to obtain a palindrome. For example:

    • Input 56 → 56+65=121 → 1 operation.

    • Input 57 → 57+75=132 → 132+231=363 → 2 operations.

    2. Program Structure Analysis

    Preparation Stage

    • <span>#include<iostream></span>: Equivalent to preparing paper and pen, allowing the program to input and output.

    • <span>using namespace std</span>: Equivalent to saying “the cin/cout I write later are from the standard library”.

    Variable Explanation

    • <span>n</span>: The number provided by the user (which will change continuously).

    • <span>ni</span>: The appearance of the number after reversal.

    • <span>ce</span>: Temporary variable, helps with the reversal calculation.

    • <span>ji</span>: A “notebook” for recording the number of operations (starting from 0).

    Core Algorithm

    1. Number reversal (like looking in a mirror):

    • <span>ce%10</span>: Extract the last digit (123%10=3).

    • <span>ce/10</span>: Remove the last digit (123/10=12).

    • <span>ni=ni*10+digit</span>: Append the digit to the right of ni.

  • Palindrome check:

    • Compare <span>n</span> and <span>ni</span> for equality.

    • If different, add:<span>n = n + ni</span>

    3. Example of Execution Flow

    Assuming input is 57:

    1. First loop:

    • Reverse 57→75.

    • 57≠75 → Add to get 132.

    • ji=1.

  • Second loop:

    • Reverse 132→231.

    • 132≠231 → Add to get 363.

    • ji=2.

  • Third loop:

    • Reverse 363→363.

    • 363=363 → Stop.

  • Output ji-1=1 (this may be controversial, see notes).

  • 4. Important Details

    • Why use ji-1: Because the first check has not performed any addition operation yet.

    • while(n!=1): This is a special setting; normally it should be <span>while(n!=ni)</span>.

    • Number Reversal Principle:

      Example: Reverse 123
      Step 1: ni=0 * 10+3=3, ce=12
      Step 2: ni=3 * 10+2=32, ce=1 
      Step 3: ni=32 * 10+1=321, ce=0
      
      
      
      

    5. Cautions

    1. Potential Issues:

    • In the original code, <span>while(n!=1)</span> may be a bug; it should be <span>while(n!=ni)</span>.

    • Some numbers may require many steps to become a palindrome.

  • Improvement Suggestions:

    • A maximum step limit can be added to avoid infinite loops.

    • Add intermediate step printing for easier debugging.

    • Handle cases of negative number inputs.

    6. Learning Suggestions

    Beginners can practice as follows:

    1. Simulate the program’s operation with paper and pen (choose a small number).

    2. Try modifying the code to print each operation process.

    3. Consider: What happens if the input is 1?

    4. Challenge: How to make the program handle larger numbers?

    This program effectively demonstrates:

    • The use of loop structures.

    • Techniques for manipulating digit positions.

    • The basic ideas of algorithm design.

    I hope this explanation helps you fully understand! If you have any questions, I recommend running the program and observing its behavior.

    Leave a Comment