# Problem: Leet Code 9 - Palindrome Number

### **QUESTION:**

Given an integer `x`, return `true` *if* `x` *is a* ***palindrome****, and* `false` *otherwise*.

**Example 1:**

```plaintext
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
```

**Example 2:**

```plaintext
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
```

**Example 3:**

```plaintext
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
```

**Constraints:**

* `-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1`
    

**Follow up:** Could you solve it without converting the integer to a string?  

### 💭**Analyzing question:**

* Concept of palindrome:
    
    The given input on reversing results in the same input again when compared is said to be a palindrome
    
* Concept of Number Palindrome:  
    To reverse the given number and check whether the reverse is same as the given number, if same we say it as palindrome else not a palindrome.
    
* Concept of operators:  
    Modulo Operator (%) - Gives remainder on dividing  
    Division Operator ( / ) - Gives quotient on dividing
    

### **💡Approach:**

1. To first get number input from user
    
2. Store it in a temporary variable as modification will be made to the given input directly
    
3. To reverse number using while loop, the clear-cut methodology is attached below to understand best of it.
    
4. This is how in loop number gets reversed and the output is checked based on the return type in case of function, else using conditional statement too.
    
5. We finally compare and return the nature of the given number.
    

### 😮‍💨**ATTACHED DETAILED WORKING:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751478033027/49540135-bab1-401b-b708-ed77729cfb4a.jpeg align="center")

### 💻**My JAVA Code:**

```java
class Solution {
    public boolean isPalindrome(int x) {
        int org = x;
        int rev = 0;
        while(x > 0){
            int rem = x % 10;
            rev=rev*10+rem;
            x /= 10;
        }
        return org==rev;
    }
}
```

### 🖥️**Output:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751478085884/a5585dea-7f4e-4a02-b89d-fbaa0ea84822.png align="center")

### **⏱️Efficiency of my approach:**

* Time Complexity: O(log x); where x denotes the number of digits in the number
    
* Space Complexity: O(1)
    

### **🧠My Learnings:**

1. Recap on the fundamental of Operators
    
2. Insights on the logic of reversing a number
    

### **Tags:**

#Java #Leetcode #ProblemSolving #DSA
