i'm trying to write a program that when given any number it can generate a palindrome by adding reverse()to the number and also given the number of times it was added to get to the palindrome. considring using Integer.Max_Value thank you

say, 48. Add 48 to its reversal, 84; we get 48 + 84 = 132. Since 132 is not a palindrome, we add it to its reversal to get 132 + 231 = 363 and we stop, having reached a palindrome. Write a Java console application that reads an integer (of type int) and applies the preceding process zero or more times until a palindrome results. Your application should count the number of times a number must be added to its reversal to reach a palindrome. Be sure the loop stops before an integer occurs that is too large for type int on your computer; for example, starting with 89 will require integers larger than Integer.MAX_VALUE and so will be impossible using the data type int. If a palindrome can not be generated, your program needs to display a message indicating that and the number of times the number was added to its reversal until an overflow occurred. thank you

muath, you'll probably need to use a couple of StringBuffers to make the reversing easy and Integer.parseInt() on it. You can do without checking integer.max_value since it wouldn't help anyways. Your value would always be less. Just keep a variable that has the last value in it. compare the last and current values. When the current value is less(it has rolled over to negatives) then you reached the end. You'll also need to keep a counter of reverses. It should be fairly straight forward once you get into it. heh, Barry can you show him how to write one that will find a palindrome for 196 ? (Yes check that link, this problem is like mathematical crack cocaine to some people) [ March 12, 2003: Message edited by: Chris Shepherd ]

muath khalil
Greenhorn

Joined: Mar 11, 2003
Posts: 5

posted

0

i tried using m=input%10/(10/10) ; n=(input%100)/(100/10); o=(input%1000)/(1000/10); p=(input%10000)/(10000/10); so now if i have an input of 1234 ..the out put is 4321 but how can i save that out put so i can add it to the input ...is this a bad idea ... thank you ..

muath khalil
Greenhorn

Joined: Mar 11, 2003
Posts: 5

posted

0

there is an easier way by usieng the reverse methode can some one explain ...

What if the user types a 5-digit number in? Then your reversal won't quite work. Notice that each line looks remarkably similar. This is a clue that you can use a loop to repeat the process as many times as necessary, therefore accepting numbers of any arbitrary length. Can you see how to make such a loop?

Thanks for the example and a nice clear problem specification. Thanks, too, Chris for the link. John Walker's got some real interesting stuff on his formilab site. 196? I'll start tomorrow

What fun! 89 gives rise to 8813200023188L which is bigger than Integer.MAX_VALUE. And 196 blows up my program using longs very quickly. [ March 12, 2003: Message edited by: Barry Gaunt ]

muath khalil
Greenhorn

Joined: Mar 11, 2003
Posts: 5

posted

0

Thank you for you help... this code is like a code i have ... public int sumDigits(int n) { int rmd; while(n!=0) { rmd = n%10; n = n/10; sum = sum+rmd; } } return sum; but isn't there a way to use a for loop insted .. this is my code but i'm not sure of it .. import java.io.*; import java.lang.*; public class Palindromes { public static void main(String[]args)throws IOException { String reverse; System.out.print("please Enter a number: "); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine(); int input = Integer.parseInt(s); public int sumDigits(int n) { int rmd; while(n!=0) { rmd = n%10; n = n/10; sum = sum+rmd; } } return sum;

The best way to find out what your code does is to compile and run it. Print out the values each time through the while loop. I can clearly see that your code will not even compile (for more than one reason).

Don't get me started about those stupid light bulbs.