{"id":6242,"date":"2025-05-30T20:02:20","date_gmt":"2025-05-30T14:32:20","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6242"},"modified":"2025-05-30T20:02:20","modified_gmt":"2025-05-30T14:32:20","slug":"check-if-a-number-is-a-palindrome","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/check-if-a-number-is-a-palindrome\/","title":{"rendered":"Check if a Number is a Palindrome"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n\n<style>\n .wp_blog_code-tabs-container {\n   font-family: \"Segoe UI\", sans-serif !important;\n   max-width: 900px !important;\n   margin: 2rem auto !important;\n   border: 1px solid #ddd !important;\n   border-radius: 8px !important;\n   overflow: hidden !important;\n   background-color: white !important;\n }\n\n\n .wp_blog_code-tabs-header {\n   background: #f7f7f7 !important;\n   display: flex !important;\n   border-bottom: 1px solid #ddd !important;\n }\n\n\n .wp_blog_code-tab-button {\n   flex: 1 !important;\n   padding: 10px 15px !important;\n   border: none !important;\n   background: transparent !important;\n   cursor: pointer !important;\n   font-weight: bold !important;\n   transition: background 0.2s !important;\n   color: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-button.active {\n   background: white !important;\n   border-bottom: 3px solid #0073aa !important;\n }\n\n\n .wp_blog_code-tab-content {\n   display: none !important;\n   padding: 20px !important;\n   background: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-content > pre{\n   background: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-content.active {\n   display: block !important;\n }\n\n\n .wp_blog_code-tab-content pre {\n   margin: 0 !important;\n   overflow-x: auto !important;\n }\n\n\n .wp_blog_explanation {\n   max-width: 900px !important;\n   margin: 2rem auto !important;\n   font-family: \"Segoe UI\", sans-serif !important;\n   line-height: 1.6 !important;\n   background: white !important;\ncolor : black !important;\n   padding: 1rem !important;\n   border-radius: 8px !important;\n }\n\n\n .wp_blog_explanation h2 {\n   color: #0073aa !important;\n   font-size: 1.5rem !important;\n   margin-bottom: 0.5rem !important;\n }\n\n\n .wp_blog_explanation code {\n   background: #f1f1f1 !important;\n   padding: 2px 6px !important;\n   border-radius: 4px !important;\n   font-family: monospace !important;\n }\n\n\n.wp_blog_explanation h1,\n.wp_blog_explanation h2,\n.wp_blog_explanation h3,\n.wp_blog_explanation h4,\n.wp_blog_explanation h5,\n.wp_blog_explanation h6,\n.wp_blog_explanation p {\n margin-top: 10px !important;\n margin-bottom: 10px !important;\n}\n<\/style>\n\n\n<div class=\"wp_blog_explanation\">\n  <p>Write a function <code>isPalindrome(x)<\/code> that takes an integer <code>x<\/code> and returns <code>true<\/code> if it reads the same backward and forward; otherwise <code>false<\/code>.<\/p>\n\n  <h2>Requirements<\/h2>\n  <ul>\n    <li>Handles both positive and negative integers.<\/li>\n    <li>Returns <code>false<\/code> for negative numbers (not palindromes).<\/li>\n  <\/ul>\n\n  <h2>Constraints<\/h2>\n  <ul>\n    <li>Time Complexity: <code>O(d)<\/code> where <code>d<\/code> is the number of digits.<\/li>\n    <li>Space Complexity: <code>O(1)<\/code> \u2014 Only a few variables are used.<\/li>\n  <\/ul>\n\n  <h2>Examples<\/h2>\n  <pre><code>Input: 121\nOutput: true\n\nInput: -121\nOutput: false\n\nInput: 10\nOutput: false<\/code><\/pre>\n\n  <h2>Step-by-Step Approach<\/h2>\n  <ul>\n    <li><strong>Handle Negatives:<\/strong> If <code>x &lt; 0<\/code>, return <code>false<\/code>.<\/li>\n    <li><strong>Store Original:<\/strong> Save the input in <code>xCopy<\/code> for comparison.<\/li>\n    <li><strong>Reverse:<\/strong>\n      <ul>\n        <li>Initialize <code>rev = 0<\/code>.<\/li>\n        <li>While <code>x &gt; 0<\/code>:\n          <ul>\n            <li><code>rem = x % 10<\/code><\/li>\n            <li><code>rev = rev * 10 + rem<\/code><\/li>\n            <li><code>x \/\/= 10<\/code><\/li>\n          <\/ul>\n        <\/li>\n      <\/ul>\n    <\/li>\n    <li><strong>Compare:<\/strong> If <code>rev === xCopy<\/code>, return <code>true<\/code>; else <code>false<\/code>.<\/li>\n  <\/ul>\n<\/div>\n\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nvar isPalindrome = function(x) {\n  if (x < 0) return false;\n  let xCopy = x;\n  let rev = 0;\n  while (x > 0) {\n    let rem = x % 10;\n    rev = rev * 10 + rem;\n    x = Math.floor(x \/ 10);\n  }\n  return rev === xCopy;\n};\n\nconsole.log(isPalindrome(121)); \/\/ true\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\n#include &lt;iostream&gt;\nusing namespace std;\n\nclass Solution {\npublic:\n  bool isPalindrome(int x) {\n    if (x < 0) return false;\n    int xCopy = x;\n    long long rev = 0;\n    while (x > 0) {\n      int rem = x % 10;\n      rev = rev * 10 + rem;\n      x \/= 10;\n    }\n    return rev == xCopy;\n  }\n};\n\nint main() {\n  Solution sol;\n  cout &lt;&lt; boolalpha &lt;&lt; sol.isPalindrome(121) &lt;&lt; endl; \/\/ true\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\n#include &lt;stdio.h&gt;\n#include &lt;stdbool.h&gt;\n\nbool isPalindrome(int x) {\n  if (x < 0) return false;\n  int xCopy = x;\n  long long rev = 0;\n  while (x > 0) {\n    int rem = x % 10;\n    rev = rev * 10 + rem;\n    x \/= 10;\n  }\n  return rev == xCopy;\n}\n\nint main() {\n  int num = 121;\n  printf(\"%s\\n\", isPalindrome(num) ? \"true\" : \"false\");\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\npublic class Solution {\n  public boolean isPalindrome(int x) {\n    if (x < 0) return false;\n    int xCopy = x;\n    int rev = 0;\n    while (x > 0) {\n      int rem = x % 10;\n      rev = rev * 10 + rem;\n      x \/= 10;\n    }\n    return rev == xCopy;\n  }\n\n  public static void main(String[] args) {\n    Solution sol = new Solution();\n    System.out.println(sol.isPalindrome(121)); \/\/ true\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\nclass Solution:\n    def isPalindrome(self, x):\n        if x < 0:\n            return False\n        x_copy = x\n        rev = 0\n        while x > 0:\n            rem = x % 10\n            rev = rev * 10 + rem\n            x \/\/= 10\n        return rev == x_copy\n\nsol = Solution()\nprint(sol.isPalindrome(121))  # True\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n\n\n<script>\n document.addEventListener('DOMContentLoaded', function () {\n   const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n   const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n\n\n   buttons.forEach(button => {\n     button.addEventListener('click', () => {\n       const lang = button.getAttribute('data-lang');\n\n\n       buttons.forEach(btn => btn.classList.remove('active'));\n       button.classList.add('active');\n\n\n       contents.forEach(content => {\n         content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n       });\n     });\n   });\n });\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Write a function isPalindrome(x) that takes an integer x and returns true if it reads the same backward and forward; otherwise false. Requirements Handles both positive and negative integers. Returns false for negative numbers (not palindromes). Constraints Time Complexity: O(d) where d is the number of digits. Space Complexity: O(1) \u2014 Only a few variables<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,811,810,174,172,173],"tags":[],"class_list":["post-6242","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6242"}],"version-history":[{"count":3,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6242\/revisions"}],"predecessor-version":[{"id":6251,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6242\/revisions\/6251"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}