Results 1 to 3 of 3
- 10-18-2011, 02:42 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
How to know if an integer is a palindrome?
hi guys. I'm stuck on some coding.
Without using strings and array, i am trying to reverse the number so i can test if it is a palindrome number( which means when an integer is in reversed order, it is equal to the original order)
what i have so far:
Java Code:public static int getPal(int num){ int i; int digit; int rem; double sum=0; double length=0; double n; length= findLength(num); for (i=0;i<num; i++){ if (i<10){ System.out.println(""+i); } } while (num>10){ n=num; while (n>0){ rem=x%10; sum=rem*length+sum; length= length/10; n=n/10; } if (num ==sum) { System.out.println(""+n); } num--; } return 0; } public static double findLength(int num){ double length=0; while (num>0){ num= num/10; length++; } length= Math.pow(10,length-1); return length; }
- 10-18-2011, 02:50 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: How to know if an integer is a palindrome?
Does the program do what you expect? If not describe both the expected and actual behaviour when you give it some specific input. If it does not compile post the exact and entire compiler message and say which line(s) of your code it is referring to.
-----
Perhaps you could also describe *how* you are deciding whether the input is a palindrome. In particular the println() statements seem a little mysterious. It is important to have a (comprehensive and specific) plan before rushing into code.
- 10-18-2011, 03:12 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 31
- Rep Power
- 0
Re: How to know if an integer is a palindrome?
Okay, i fixed it. The problem was the while loop and me using doubles instead of ints.
Java Code:public static int getPal(int num){ int i; int digit; int rem; int x; int sum; int length=0; int n; System.out.println(""); for (i=0;i<num; i++){ if (i<10){ System.out.println(""+i); } } while (num>10){ n=num; sum=0; length= findLength(num); while (n>0){ rem=n%10; sum=rem*length+sum; length= length/10; n=n/10; if (num ==sum) { System.out.println(""+sum); } } num--; } return 0; } public static int findLength(int num){ double length=0; int i=1; while (num>10){ num= num/10; length++; } while (length>0){ i = i*10; length--; } return i; }
Similar Threads
-
Integer Comparison, Outputting Largest Integer Not Working
By killingthemonkey in forum New To JavaReplies: 4Last Post: 10-16-2011, 08:59 PM -
palindrome
By rochellelising in forum New To JavaReplies: 1Last Post: 08-24-2011, 01:03 PM -
Palindrome
By leepikamukharji in forum New To JavaReplies: 2Last Post: 04-29-2011, 03:20 PM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
HELP...Palindrome
By d7o0om in forum New To JavaReplies: 12Last Post: 11-13-2009, 03:32 AM
Bookmarks