0

I need help with a method to check if a number is symmetric, so from what I understand I need to check equality between all the numbers and to make sure there is no different number amounts them...right?

This is my code:

public boolean isSemetric (int number) {

    int temp;
    boolean answer = true;

    while (number != 0) {

        temp = number % 10;
        number /= 10;

        if (temp != (number%10)) {
        answer = false;
        } else { 
            answer = true;
        }

    }

    return answer;

}

I'm kind of new to programming so be forgiven to my code :/

Thanks!

user804968
  • 161
  • 1
  • 1
  • 14
  • I don't see anything wrong with your code. The last else part seems redundant though. –  Dec 06 '13 at 23:56
  • 2
    By a symmetric number you mean palindrome? – mcserep Dec 06 '13 at 23:57
  • 1
    Don't you want to return immediately if the number isn't symmetric? – Matt Ball Dec 06 '13 at 23:57
  • 1
    "I don't see anything wrong with your code". What right do you see? This code seems to always return false. Unless number=0. – peter.petrov Dec 06 '13 at 23:59
  • yes i guess they mean palindrome...so 555 would be symmetric and 535 is symmetric, but 5345 isn't symmetric...now i see that it's totally wrong :/ @CMate – user804968 Dec 07 '13 at 00:02
  • You are checking whether every 2 following digits are the same. That is not the definition of palindrome. And you are also checking that incorrectly, because you do not break the loop if a mismatch is found. See peter.petrov's comment. – mcserep Dec 07 '13 at 00:04
  • Check this post : http://stackoverflow.com/questions/199184/how-do-i-check-if-a-number-is-a-palindrome – notArefill Dec 07 '13 at 00:04

4 Answers4

4

As peter.petrov pointed out in the comment section of your question, your method as it's written will always return false, except for when number is equal to 0. The reason for this can be seen when you pass in a number like 111 and step through the code in a debugger. The final iteration will fail because number /= 10 will result in 0, and temp will be 1, which fails your test.

If you are indeed looking to identify palindromes, consider the following approach that should be simple to implement

  1. copy number into temp
  2. convert temp to a String, and reverse it (tmpStr)
  3. convert tmpStr back to an integer (reversedInt)
  4. compare number and reversedInt for equality

viola. Not the most efficient algorithm, but its easy to understand and gets the job done.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
1

I would do it like this (I don't want to use String here and I don't want to use local array variable for the digits).

    public static boolean isSymmetric (long number) {
        if (number == 0) return true;
        else if (number < 0) return false;
        long DEG_10 = (long)(Math.pow(10, (int)Math.log10(number)));

        while (number > 0){
            long dStart = number / DEG_10;
            long dEnd = number % 10;
            if (dStart != dEnd) return false;
            number = (number - dStart * DEG_10 - dEnd) / 10;
            DEG_10 /= 100;
        }

        return true;
    }
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

Here's a somewhat fixed up version of your code. However, it checks if all numbers are the same, e.g. 5555 or 111. 11211 would return false.

public boolean areAllDigitsTheSame (int number) {

    int temp;
    boolean answer = true;

    while (number >= 10) {

        temp = number % 10;
        number /= 10;

        if (temp != (number%10)) {
            return false
        }
    }

    return true;
}

Edit: The C++ answer in the linked question works by constructing the reverse number gradually in the loop, and finally checking if they're the same. This is a similar to what you are doing.

Here's another version for fun:

public boolean isPalindrome (int number) {

    int reversedNumber = 0;
    while (number > 0) {

        int digit = number % 10;
        reversedNumber = reversedNumber*10 + digit;

        if (reversedNumber == number) { // odd number of digits
            return true;
        }

        number /= 10;

        if (reversedNumber == number) { // even number of digits
            return true;
        }
    }

    return false;
}
Community
  • 1
  • 1
johv
  • 4,424
  • 3
  • 26
  • 42
0

This is the easiest approach I can think of - Get the String value of the number, if the reverse is the same then it is symmetrical.

// Symmetry
public static boolean isSymmetric(int number) {
  String val = String.valueOf(number); // Get the string.
  StringBuilder sb = new StringBuilder(val);
  return (val.equals(sb.reverse().toString())); // if the reverse is the same...
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249