Friday, January 29, 2016

Palindrome Number

Palindrome Number


Problem: Check whether given number is a palindrome or not. Let’s do it without extra space.
Solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
boolean isPalindromeNumber(int n)
    {  
                if (n < 0) return false;
                int div = 1;
                while (n/div >= 10) {
                    div *= 10;
                }                  
        while(n != 0){
            int left = n/div;
            int right = n%10;
            if(left != right) {
                return false;
            }
            n = (n%div)/10;
            div /= 100;
         }     
        return true;
      }
Click here for more Programming Interview Questions


No comments:

Post a Comment