Learning C++ Programming from Scratch, Day 402: Palindromic Numbers; Problem Set Answers; Third Method

1083 – Palindromic Numbers

Learning C++ Programming from Scratch, Day 402: Palindromic Numbers; Problem Set Answers; Third Method

This program addresses an interesting mathematical problem: How many steps does it take to turn a number into a palindromic number by continuously adding it to its reverse?

What is a palindromic number?

It is a number that reads the same forwards and backwards, like “12321”.

How does the program work?

  1. First, we write a small utility (<span>fun</span> function) that helps us reverse a number. For example, inputting 123 will return 321.

  2. Then, we prompt the user to input a number (for example, 57).

  3. We start checking: Is this number a palindromic number?

  • If not, we perform addition: number + its “reverse” (57 + 75 = 132)

  • Record this as step 1

  • Check if the new number (132) is a palindromic number?

    • 132 + 231 = 363, this is step 2

    • Now 363 is a palindromic number, stop!

  • Finally, inform the user: it took 2 steps

  • Why is it designed this way?

    • Using the <span>fun</span> function specifically for reversing numbers makes the main program clearer

    • Using a <span>while</span> loop to continuously check until a palindromic number is obtained

    • Using the <span>c</span> variable as a counter to record how many times addition has been performed

    Reference Code

    #include<bits/stdc++.h> // Include all standard library headers to avoid the hassle of including them individually
    using namespace std;    // Use the standard namespace, allowing direct use of cout/cin, etc.
    // Define the function to reverse a number
    int fun(int n){    int r = 0; // Variable to store the reversed number
        // Reverse the number using short division
        while(n != 0){          // Continue while n has not been fully divided
            r = r * 10 + n % 10; // Add the last digit of n to r
            n = n / 10;         // Remove the last digit from n
        }    return r; // Return the reversed number}
    int main() {    // Test code, can be removed in the final program
        // cout << fun(123); // Should output 321
        int n, c = 0; // n is the user input number, c is the counter (step count)
        cin >> n;     // Get the number from the user
        // Continue looping as long as n is not a palindromic number
        while(n != fun(n)){        c++;             // Increment step count
            n = n + fun(n);  // Add the number and its reverse
        }
        cout << c; // Output the required number of steps
        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 a given number to obtain a palindromic number. For example:

    • Input 56 → 56 + 65 = 121 → 1 step

    • Input 57 → 57 + 75 = 132 → 132 + 231 = 363 → 2 steps

    2. Core Function Analysis

    <span>fun</span> function – Number Reverser:

    • Working principle: Like peeling an onion, take each digit from right to left

      • <span>n % 10</span> gets the last digit (for example, 123 % 10 = 3)

      • <span>n / 10</span> removes the last digit (123 / 10 = 12)

      • <span>r = r * 10 + digit</span> appends the digit to the right

    • Example:

      • Input: 123

      • Process:

    1. r = 0 * 10 + 3 = 3, n = 12

    2. r = 3 * 10 + 2 = 32, n = 1

    3. r = 32 * 10 + 1 = 321, n = 0 → return 321

    3. Main Program Flow

    Preparation Stage

    • <span>int n, c=0</span>: Create two “boxes”

      • n: stores the number given by the user

      • c: records the number of additions, initially 0

    Input Number

    • <span>cin >> n</span>: Prompt the user to input a number

    Palindromic Check Loop

    • <span>while(n != fun(n))</span>: Check if the current number is a palindromic number

      • <span>c++</span>: Record another addition

      • <span>n = n + fun(n)</span>: Add the number to its “reflection”

      • When it is not a palindromic number:

    • This loop will continue until the number becomes palindromic

    Output Result

    • <span>cout << c</span>: Inform the user how many steps are needed

    4. Important Details

    • Why use a while loop: Because we do not know how many times we need to add, we must keep trying until a palindromic number appears

    • Purpose of c: Like counting jumps in a physical education class, increment by 1 each time you jump

    • Palindromic Check Technique: Directly compare the number with its reverse to see if they are the same

    • Number Reversal Principle: Utilize the property of integer division that removes the decimal point

    5. Actual Running Example

    User input: 196

    Calculation process:

    1. 196 + 691 = 887 → c=1

    2. 887 + 788 = 1675 → c=2

    3. 1675 + 5761 = 7436 → c=3

      … (may take many steps, some numbers require many steps)

    6. Notes

    • Not all numbers can quickly become palindromic; some require many steps

    • If the number is too large, it may exceed the int range (consider using long long)

    • This program does not handle negative number inputs

    7. How to Test Yourself

    You can modify the code to add a line in the loop:

    cout &lt;&lt; "Step " &lt;&lt; c &lt;&lt; ": " &lt;&lt; n &lt;&lt; " + " &lt;&lt; fun(n) &lt;&lt; " = " &lt;&lt; n + fun(n) &lt;&lt; endl;

    This way, you can see the calculation process at each step.

    I hope this explanation helps beginners fully understand this program! If there are still unclear points, you can try to follow the program step by step with pen and paper.

    Leave a Comment