File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes a problem with Palindromes please help.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "a problem with Palindromes please help.." Watch "a problem with Palindromes please help.." New topic
Author

a problem with Palindromes please help..

muath khalil
Greenhorn

Joined: Mar 11, 2003
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

Joined: Aug 03, 2002
Posts: 7729
Interesting problem. Can you give an actual example using numbers?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
muath khalil
Greenhorn

Joined: Mar 11, 2003
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

Joined: Jun 27, 2000
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

Joined: Mar 11, 2003
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

Joined: Mar 11, 2003
Posts: 5
there is an easier way by usieng the reverse methode can some one explain ...
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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?


Java API Documentation
The Java Tutorial
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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

Joined: Jul 27, 2001
Posts: 1365
Perhaps this code will give you some ideas:
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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

Joined: Mar 11, 2003
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;
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;

}
}
[ March 12, 2003: Message edited by: muath khalil ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a problem with Palindromes please help..