0

I've looked at a few different stack questions and googled, but nothing I've read really has dealt with reversal of integers, but just strings.

So right now my code may or may not work at all, and it may be the dumbest thing you've ever seen, and that's okay and corrections are welcomed, but from what I hope my code will be doing is going through 100 - 999 multiplying the two ints and then checking whether it's palindromic or not. The if with reverse.equals(sum) is totally pseudocode and obviously won't work, however I can't figure out how to do a check for a palindromic int. Is there a simple way to do this? I've read some pretty lengthy and complicated ways, but I'm sure there's gotta be a simple way. Maybe not. :/. Anyway, here's my code.

public class PalandromicNum {
    public static void main(String[] args){
        int numOne = 100;
        int numTwo = 100;
        int toteVal;
        int counter = 1000;
        int sum = 0;
        int finalSum = 0;

        for(int i=0; i<counter; i++){
            toteVal = numOne * numTwo;
            numTwo++;

            if(numTwo == 999){
                numOne++;
                numTwo = 100;
            }

            if(toteVal < sum){
                sum += toteVal;
                if(reverse.equals(sum)){
                    finalSum = sum;
                    System.out.println(finalSum);
                }
            }
        }
    }
}

Thanks again in advance!

Kristaphonie
  • 99
  • 2
  • 11

4 Answers4

2

This is on my phone so sorry for any errors.

Convert your number to a String and:

public static boolean isPalindrome(String str)
{
    // base recursive case
    if (str.length <= 1) {
        return true;
    }
    // test the first and last characters
    char firstChar = str.charAt(0);
    char lastChar = str.charAt(str.length - 1)  // subtract 1 as indexes are 0 based
    if (!firstChar.equals(lastChar)) {
        return false;
    }
    // if the string is longer than 2 chars and both are equal then recursively call with a shorter version
    // start at 2nd char, end at char before last
    return isPalindrome(str.substring(1,str.length);
}
indivisible
  • 4,892
  • 4
  • 31
  • 50
0

Reversing integers is quite easy. Remember mod 10 gives u last digit. Loop over it, chopping off last digit of the number one at a time and adding it to reverse to new number. Then its matter of simple integer equality

int rev = 0;
int n = sum;
while(n)
{
   rev = rev*10 + n%10;
   n /= 10;
}
if(sum==rev)
   //palindrome
else
   //no no no no.
GoldRoger
  • 1,263
  • 7
  • 10
0

You can create a function named isPalindrome to check whether a number is a palindrome. Use this function in your code. You just need to pass the number you want to check into this function. If the result is true, then the number is a palindrome. Else, it is not a palindrome.

public static boolean isPalindrome(int number) {
            int palindrome = number; // copied number into variable
            int reverse = 0;

            while (palindrome != 0) {
                int remainder = palindrome % 10;
                reverse = reverse * 10 + remainder;
                palindrome = palindrome / 10;
            }

            // if original and reverse of number is equal means
            // number is palindrome in Java
            if (number == reverse) {
                return true;
            }
            return false;
        }

    }
Lawrence Wong
  • 1,129
  • 4
  • 24
  • 40
0

I believe that this code should help you out if you are trying to find out how many palindromes are between 100-999. Of course, it will count palindromes twice since it looks at both permutations of the palindromes. If I were you I would start creating methods to complete a majority of your work as it makes debugging much easier.

  int total = 100;
  StringBuilder stringSumForward;
  StringBuilder stringSumBackward;
  int numberOfPals = 0;


  for(int i = 100; i < 999; i++){
     for(int j = 100; j < 999; j++){
        total = i * j;
        stringSumForward = new StringBuilder(String.valueOf(total));
        stringSumBackward = new StringBuilder(String.valueOf(total)).reverse();

        if(stringSumForward.toString().equals(stringSumBackward.toString())){
           numberOfPals++;
        }
     }
  }
Peter Kaminski
  • 822
  • 1
  • 12
  • 21