public class reverseNumber
{
public void func(int n)
{
int d,r; r=0;
do
{
d=n%10; // returns remainder
r=r*10+d;
n=n/10; // returns quotient
}while(n!=0);
if(n==r)
System.out.println("palindrome");
else
System.out.println("not palindrome");
}
}
4 replies to this topic
#1
Posted 12 April 2011 - 08:02 AM
Please help me in following code. The program always returns else part
|
|
|
#2
Posted 12 April 2011 - 08:17 AM
}while(n!=0); if(n==r)n must always equal 0 at the end of that do loop. Your do while loop adds value to r, so inevitably if n started > 9 then r must be > 0, therefore n will never == r. Thus, you get "not palindrome". Why are you using this method to determine a palindrome anyway, why not just take user input String object, make a reversed string, then use .equals()?
Wow I changed my sig!
#3
Posted 12 April 2011 - 08:44 AM
Thank you ZekeDragon. I have already solved the problem with the help of the program given in the following link .
Java Palindrome Number,Java Palindrome Code,Example of Palindrome Number,Program of Java Palindrome Number
Java Palindrome Number,Java Palindrome Code,Example of Palindrome Number,Program of Java Palindrome Number
#4
Posted 13 April 2011 - 08:16 AM
I'm asking why didn't you just do this:
public boolean isPalindrome(String check)
{
return new StringBuilder(check).reverse().toString().equals(check);
}
instead of the complicated mathematical method?
Wow I changed my sig!
#5
Posted 02 May 2011 - 09:20 AM
thanks to all
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









