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.
- Use a loop to perform the reverse-and-add process.
- If the current number is already a palindrome, print it and stop.
- Otherwise, reverse it, add it, and increment a step count.
- Stop when a palindrome is reached or after 10 steps (to prevent Lychrel infinite loops).
- Print the final palindrome number.
01EXAMPLE 1
Input
num = 195Output
9339Explanation: Reaches palindrome in 4 steps.
Constraints
- Use loops for reversal and palindrome checking.
- Stop after 10 steps max.
MathLoopsDigits
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
num195
Expected Output
9339
Click RUN to test your solution