The Palindrome Sum

MediumAcc. 89.4%
+30 XP 12

Reverse-and-Add

Take a number, reverse it, and add the two. Repeat this until the result is a palindrome. Example: 195 $ o$ 195 + 591 = 786 $ o$ 786 + 687 = 1473 $ o$ 1473 + 3741 = 5214 $ o$ ...

The Assignment

Your function receives a parameter num.

  1. Use a loop to perform the reverse-and-add process.
  2. If the current number is already a palindrome, print it and stop.
  3. Otherwise, reverse it, add it, and increment a step count.
  4. Stop when a palindrome is reached or after 10 steps (to prevent Lychrel infinite loops).
  5. Print the final palindrome number.

01EXAMPLE 1

Inputnum = 195
Output9339

Explanation: Reaches palindrome in 4 steps.

Constraints

  • Use loops for reversal and palindrome checking.
  • Stop after 10 steps max.
MathLoopsDigits
JavaScript
Loading...
3 Hidden

Input Arguments

num195

Expected Output

9339

Click RUN to test your solution