I need to create a numerical palindrome program

5 replies on 1 page. Most recent reply: Jul 26, 2007 2:24 PM by Sunil P

 Previous Topic Next Topic
 Flat View: This topic has 5 replies on 1 page
 Dustin Posts: 1 Nickname: jeeper Registered: Jun, 2003
I need to create a numerical palindrome program Posted: Jun 15, 2003 5:28 PM
It needs to take in a number thenif it is a palindrome print out true if it is not it needs to add that number to its self and then check if it is a palindrome if it isn't recheck that and see if it is a palindrome reapet ten time then print out false

ex 155 = false so
155+551
706 = false
706 + 607
1313 + false ten times or until it become a palindrome thanks

 E. Nielsen Posts: 9 Nickname: en Registered: Feb, 2003
Re: I need to create a numerical palindrome program Posted: Jun 16, 2003 3:01 PM
Just do it!

.->

 Mr Plow Posts: 18 Nickname: mrplow Registered: Jun, 2003
Re: I need to create a numerical palindrome program Posted: Jun 16, 2003 4:58 PM
Not the prettiest code, but it works.

`public class Palindrome{  public static void main(String[] args)  {    //The number to be tested...    int x = 155;    //Loop through a maximum of 10 times 	for(int i = 1 ; i <= 10; i++)	{      //check if palindrome, if yes, print out result and break out of loop	  if(isPali(x)) 	  {System.out.println(x + " is a palindrome\nIt took " + i + " times through the for loop to get this result."); break;}	  	  //check to make sure that you haven't already looped 10 times.	  if(i == 10)	  {	    System.out.println("After 10 iterations, this number: " + x + " is still not a palindrome... exiting");		break;	  }	  	  //reverse the number and add the reversed number to the orginal.  Also print out what the program is doing so that the user	  //can follow what is happening	  StringBuffer sb = new StringBuffer(x + "");	  StringBuffer sb2 = sb.reverse();	  String s3 = sb2.toString();	  int x2 = Integer.parseInt(s3);	  System.out.println("x = " + x);	  System.out.println("x reversed = " + x2);	  x += x2;	  System.out.println("x + x reversed = " + x);	  	}//end of for loop  }//end of main() method  //static method that can be called to see if a given number is a palidrome  public static boolean isPali(int l)  {    String longAsString = l + "";	int x = longAsString.length();		//logic to handle single digit input	if (x == 1)	{	  return true;	}    //logic to handle double digit input    if(x == 2)	{	  char x1 = longAsString.charAt(0);	  char x2 = longAsString.charAt(1);	  return x1 == x2;	}	//logic to handle input with odd number of digits	if( (x % 2) == 1)	{	  //Remove middle char which doesn't need to match	  StringBuffer newString = new StringBuffer();	  int xx = x;	  int y = (xx / 2);	  newString.append(longAsString.substring(0, y));	  newString.append(longAsString.substring(y + 1));	  String ss = newString.toString();	  int xxx = ss.length();	  for(int i = 0; i < xxx/2; i++)	  {	    char xFirst = ss.charAt(i);		char xLast = ss.charAt(xxx - (i + 1));		if(xFirst != xLast)		{		  return false;		}	  }	  return true;	  	}//end of if odd number of digits.	//logic to handle input with even number of digits    if( (x % 2) == 0)	{	  int xx = longAsString.length();      for(int i = 0; i < xx/2; i++)	  {	    char xFirst = longAsString.charAt(i);		char xLast = longAsString.charAt(xx - (i + 1));		if(xFirst != xLast)		{		  return false;		}	  }	  return true;	}//end of if even number of digits	//default return value    return false;  }};`

 Raja Datla Posts: 1 Nickname: datla Registered: Jul, 2007
Re: I need to create a numerical palindrome program Posted: Jul 2, 2007 12:42 AM
for Logic 2 why can't we directly send true .. the palindrome of two numbers is always true. right ?

 Sunil P Posts: 2 Nickname: sunlee79 Registered: Jul, 2007
Re: I need to create a numerical palindrome program Posted: Jul 26, 2007 2:23 PM
public class Palindrome {

public static void main(String args[]) {

int numberInput = Integer.parseInt(args[0]);
int paliNum = 0;
boolean isNumberPali = false;
System.out.println("input number was : " + numberInput);

for (int j = 0 ; j < 10 ; j++) {

if(isPalindrome(numberInput)) {
isNumberPali = true;
paliNum = numberInput;
break;
}

numberInput += numberInput;
System.out.println("new number is : " + numberInput);

}

if(isNumberPali == true) {
System.out.println(paliNum + " is palindrome!!!");
}
else {
System.out.println("not a palindrome!!!");
}
}

protected static boolean isPalindrome (int x) {

int y = getReverse(x);
if (y == x) {
return true;
}

return false;
}

protected static int getReverse (int x) {

int y = 0;
while (x > 0) {
y = y * 10;
y = y + x%10;
x = x/10;
}

return y;
}
}

 Sunil P Posts: 2 Nickname: sunlee79 Registered: Jul, 2007
Re: I need to create a numerical palindrome program Posted: Jul 26, 2007 2:24 PM
@Raja,
palindrome of 1 number is always true ... 53 is not a palindrome. i hope I understood what you said!

 Flat View: This topic has 5 replies on 1 page
 Previous Topic Next Topic

 Web Artima.com