This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

muath khalil
Greenhorn
Posts: 5
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

Barry Gaunt
Ranch Hand
Posts: 7729
Interesting problem. Can you give an actual example using numbers?

muath khalil
Greenhorn
Posts: 5
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

Chris Shepherd
Ranch Hand
Posts: 286
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
Posts: 5
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
Posts: 5
there is an easier way by usieng the reverse methode can some one explain ...

Layne Lund
Ranch Hand
Posts: 3061
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?

Barry Gaunt
Ranch Hand
Posts: 7729
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

David Weitzman
Ranch Hand
Posts: 1365
Perhaps this code will give you some ideas:

Barry Gaunt
Ranch Hand
Posts: 7729
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
Posts: 5
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;
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;

}
}
[ March 12, 2003: Message edited by: muath khalil ]

Barry Gaunt
Ranch Hand
Posts: 7729
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).