Tutorialspoint
Problem
Solution
Submissions

Check Palindrome Number

Certification: Basic Level Accuracy: 0% Submissions: 0 Points: 5

Write a JavaScript program to determine if a given integer is a palindrome number. A palindrome number reads the same backward as forward. You should solve this without converting the integer to a string.

Example 1
  • Input: num = 121
  • Output: true
  • Explanation:
    • The number 121 reads the same from left to right and right to left.
    • When we reverse 121, we get 121.
    • Since the original number equals its reverse, it is a palindrome.
    • Therefore, the output is true.
Example 2
  • Input: num = -121
  • Output: false
  • Explanation:
    • The number -121 has a negative sign at the beginning.
    • When reversed, it would be 121- which is not a valid number format.
    • Negative numbers cannot be palindromes due to the negative sign.
    • Therefore, the output is false.
Constraints
  • -2^31 ≤ num ≤ 2^31 - 1
  • You cannot convert the integer to a string
  • Negative numbers should return false
  • Time Complexity: O(log n) where n is the input number
  • Space Complexity: O(1)
NumberControl StructuresHCL TechnologiesPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Handle negative numbers first - they cannot be palindromes
  • Use mathematical operations to reverse the number digit by digit
  • Extract the last digit using the modulo operator (num % 10)
  • Remove the last digit by integer division (Math.floor(num / 10))
  • Build the reversed number by multiplying the current reversed number by 10 and adding the extracted digit
  • Continue this process until the original number becomes 0
  • Compare the original number with the reversed number to determine if it's a palindrome

Steps to solve by this approach:

 Step 1: Check if the number is negative, if so return false immediately
 Step 2: Store the original number in a variable for later comparison
 Step 3: Initialize a variable to store the reversed number, starting with 0
 Step 4: Use a while loop to continue until the number becomes 0
 Step 5: Extract the last digit using modulo operation (num % 10)
 Step 6: Build the reversed number by multiplying current reversed by 10 and adding the extracted digit
 Step 7: Remove the last digit from the original number using integer division
 Step 8: Compare the original number with the reversed number and return the result

Submitted Code :