logo Practice-It logo

reverseAndAdd

Language/Type: Java BigInteger
Author: Marty Stepp (on 2013/01/07)

Write a method named reverseAndAdd that accepts an integer parameter and repeatedly adds the integer to the reversal of itself (the integer constructed by reversing the order of the digits) until the result is a palindrome (an integer that is the same when the order of its digits is reversed). Your method should print a single line of output containing the number of adds that were needed, followed by a space, followed by the palindrome that was computed. For example, the call of reverseAndAdd(195); should print:

4 9339

The computation performed by your method for the previous call is the following:

   195 (initial number)
+  591 
 -----  add #1
   786 
+  687 
------  add #2
  1473 
+ 3741 
------  add #3
  5214 
+ 4125 
 -----  add #4
  9339 (resulting palindrome)
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.