Results 1 to 6 of 6
Thread: Problem with Palindromes
 03072012, 11:47 AM #1Member
 Join Date
 Mar 2012
 Posts
 3
 Rep Power
 0
Problem with Palindromes
I have recently started using java and decided to try to make a palindrome program (a palindrome is where the number is the same forwards and backwards).
so far I have managed to find the inverse, but the program wont terminate when it has found the inverse, and has some strange outputs. I dont know much about java so i've probably overlooked some simple things.
import java.util.*;
import java.lang.Math;
Java Code:public class Palindrome { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Input Initial Value: "); int numPrimary = input.nextInt(); //Takes your initial input int numInverse = 0; //The variable that will become the inverse of the 'primary' int numTemp = numPrimary; //temporary variable int[] remainderArray; //array for storing the remainders int[] tempArray; //temporary array for flipping the order of the remainders int runCount = 0; //for counting how many times it takes to find a Palindrome int arrayCount = 0; //a count used for the arrays int primaryLength; //the length of the primary variable, used for array sizes int tempCount = 0; //a temporary count for inverting the remainders int mathTemp; //a temporary variable for creating the numInverse boolean numCheck; //for checking if is a palindrome do { numPrimary = numPrimary + numInverse; arrayCount = 0; tempCount = 0; //Setting size of the arrays primaryLength = Integer.toString(numPrimary).length(); remainderArray = new int[primaryLength+1]; tempArray = new int[primaryLength+1]; //finds the remainders of the primary variable do { remainderArray[arrayCount] = numTemp % 10; numTemp = numTemp / 10; arrayCount = arrayCount + 1; } while(numTemp>0); //sets an array with the elements flipped around do { tempArray[arrayCount] = remainderArray[tempCount]; tempCount = tempCount + 1; arrayCount = arrayCount  1; } while(arrayCount >0); //uses the flipped array to find the inverse do { numTemp = numInverse; mathTemp = (int)Math.pow(10, arrayCount); numInverse = numTemp + (tempArray[arrayCount]*mathTemp); arrayCount = arrayCount + 1; } while(arrayCount<=primaryLength); //recalibrates the inverse and adds the the run counter numInverse = numInverse / 10; runCount = runCount + 1; System.out.println(numInverse); }while (numPrimary != numInverse); } }
 03072012, 12:21 PM #2Moderator
 Join Date
 Apr 2009
 Posts
 11,450
 Rep Power
 18
Re: Problem with Palindromes
What output do you get and what output do you expect?
Please do not ask for code as refusal often offends.
 03072012, 12:39 PM #3Member
 Join Date
 Mar 2012
 Posts
 3
 Rep Power
 0
Re: Problem with Palindromes
It depends on the input.
ie. Input:12
Expected Output: 21
33
Actual Output: 21
3
13
34
37
50
710
72
34
246
488
6932
8932
3132
22626
28524
67110
46828
87546
873332
710670
2147084
695449
5014140
 03072012, 07:23 PM #4Member
 Join Date
 Nov 2011
 Posts
 18
 Rep Power
 0
Re: Problem with Palindromes
An easy way I made a Palindrome program was using strings rather than integers. You can make the input number a string, and add each digit of the number string to an array using the substring method. Then all you need to do is make a backwards number array that adds the substrings in the opposite direction. Then you can compare the forwards and backward arrays. If all of the elements match up in order (forward[1] = backward[1].....) , it is a palindrome.
 03142012, 11:41 AM #5Member
 Join Date
 Mar 2012
 Posts
 3
 Rep Power
 0
Re: Problem with Palindromes
Java Code:import java.util.Scanner; import java.lang.Math; public class Palindromev2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int valPrimary; int valInverse; int valLength; int valTemp; int[] aryRemainder; int cntArray; int cntMath; int cntRuns; System.out.print("Input initial Integer: "); valPrimary = sc.nextInt(); do { valInverse = 0; valLength = Integer.toString(valPrimary).length(); cntArray = valLength  1; cntMath = 0; cntRuns = 0; valTemp = valPrimary; aryRemainder = new int[valLength]; do { aryRemainder[cntArray] = valTemp % 10; valTemp= valTemp / 10; cntArray = cntArray  1; } while(cntArray>=0); do { valInverse = valInverse + (aryRemainder[cntMath]*((int)Math.pow(10,cntMath))); cntMath = cntMath + 1; } while(cntMath < valLength); cntRuns = cntRuns + 1; System.out.println("The Inverse is: " + valInverse); System.out.println(""); if (valInverse=valPrimary); { break; } valPrimary = valPrimary + valInverse; System.out.println("The new Primary is : " + valPrimary); } while(valLength>0); System.out.println("The final Palindrome is: " + valPrimary); System.out.println("It took " + cntRuns + " to find a Palindrome"); } }
 03142012, 12:01 PM #6Moderator
 Join Date
 Apr 2009
 Posts
 11,450
 Rep Power
 18
Similar Threads

Counting number of palindromes in a file
By vuzuggu in forum New To JavaReplies: 3Last Post: 10202011, 05:52 PM 
Project Euler #4  Palindromes
By Awk34 in forum New To JavaReplies: 12Last Post: 12192010, 10:28 AM 
Finding out number of Palindromes in a Sentence
By rameshiit19 in forum New To JavaReplies: 1Last Post: 09132010, 03:44 AM 
Find All Palindromes
By spleenlol in forum New To JavaReplies: 6Last Post: 02042010, 02:33 AM
Bookmarks