{"id":1989,"date":"2023-03-27T12:44:10","date_gmt":"2023-03-27T07:14:10","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=1989"},"modified":"2023-03-27T12:44:11","modified_gmt":"2023-03-27T07:14:11","slug":"palindrome-number","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/palindrome-number\/","title":{"rendered":"Palindrome Number in C, Java, and Python"},"content":{"rendered":"\n<div class=\"gutentoc tocactive ullist\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"text_open\">show<\/div><\/div><div id=\"toclist\"><div class=\"gutentoc-toc__list-wrap\"><ul class=\"gutentoc-toc__list\"><li><a href=\"#problem-statement\">Problem Statement<\/a><\/li><li><a href=\"#dry-run-of-the-above-approach\">Dry run of the above approach<\/a><\/li><li><a href=\"#implementation\">Implementation<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#cc-implementation\">C\/C++ Implementation<\/a><\/li><li><a href=\"#java-implementation\">Java Implementation<\/a><\/li><li><a href=\"#python-implementation\">Python Implementation<\/a><\/li><\/ul><li><a href=\"#special-case\">Special Case<\/a><\/li><li><a href=\"#implementation\">Implementation<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#c-implementation-of-special-case\">C Implementation of Special Case<\/a><\/li><li><a href=\"#c-implementation-of-special-case\">C++ Implementation of Special Case<\/a><\/li><li><a href=\"#java-implementation-of-special-case\">Java Implementation of Special Case<\/a><\/li><li><a href=\"#python-implementation-of-special-case\">Python Implementation of Special Case<\/a><\/li><\/ul><li><a href=\"#practice-question\">Practice Question<\/a><\/li><li><a href=\"#frequently-asked-questions\">Frequently Asked Questions<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#q1-can-a-negative-number-be-palindrome\">Q.1: Can a negative number be palindrome?<\/a><\/li><li><a href=\"#q2-can-we-solve-this-problem-without-converting-an-integer-to-a-string\">Q.2: Can we solve this problem without converting an integer to a string?<\/a><\/li><li><a href=\"#q3-are-all-single-digit-numbers-palindrome\">Q.3: Are all single-digit numbers palindrome?<\/a><\/li><li><a href=\"#q4-how-is-the-time-complexity-of-the-above-algorithm-olog-10-n\">Q.4: How is the time complexity of the above algorithm O(log<sub>10<\/sub>(N))?<\/a><\/li><\/ul><\/ul><\/div><\/div><\/div><\/div><style> .gutentoc-toc-wrap ul li a, .gutentoc-toc-title-wrap .text_open{ color: #444}} <\/style>\n\n\n\n<p>A <strong>palindrome number<\/strong> is a number that remains the same when digits are reversed. <\/p>\n\n\n\n<p>For example, the number 12321 is a palindrome number, but 1451 is not a palindrome number.<\/p>\n\n\n\n<h2 id=\"problem-statement\">Problem Statement<\/h2>\n\n\n\n<p>Given a positive integer, the task is to check whether the number is palindrome or not.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<ul><li>Input: 1221<\/li><li>Output: True<\/li><\/ul>\n\n\n\n<ul><li>Input: 146541<\/li><li>Output: False<\/li><\/ul>\n\n\n\n<ul><li>Input: 5<\/li><li>Output: True<\/li><\/ul>\n\n\n\n<p>The idea is to make a number with all the digits of the given number present in reverse order and check if both numbers are equal. Follow the below <strong>steps to solve the problem<\/strong>.<\/p>\n\n\n\n<ul><li>First, Declare a variable reverseNum and initialize it with 0.<\/li><li>Now, make a while loop till the original number is greater than zero.<\/li><li>In every loop get the last digit of the number and add that digit at the end of the reverseNum and then, divide the original number by 10.<\/li><\/ul>\n\n\n\n<p><strong><em>reverseNum = reverseNum * 10 + (number % 10)<\/em><\/strong><\/p>\n\n\n\n<ul><li>Lastly, check if the original number and reverseNum number are equal or not.<\/li><\/ul>\n\n\n\n<h2 id=\"dry-run-of-the-above-approach\">Dry run of the above approach<\/h2>\n\n\n\n<ul><li>Orginal = 1221<\/li><li>reverseNum = 0<\/li><li>tempOrginal = 1221<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>Iteration 1<\/td><td>lastDigit = 1<br>reverseNum = 0 * 10 + 1 = 1<br>tempOriginal = 122<\/td><\/tr><tr><td>Iteration 2 <\/td><td><meta charset=\"utf-8\">lastDigit = 2<br>reverseNum = 1 * 10 + 2 = 12<br>tempOriginal = 12<\/td><\/tr><tr><td>Iteration 3<\/td><td>lastDigit = 2<br>reverseNum = 12 * 10 + 2 = 122<br>tempOriginal = 1<\/td><\/tr><tr><td>Iteration 4<\/td><td>lastDigit = 1<br>reverseNum = 122 * 10 + 1 = 1221<br>tempOriginal = 0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>reverseNum = original<\/p>\n\n\n\n<h2 id=\"implementation\"><span id=\"implementation-2\">Implementation<\/span><\/h2>\n\n\n\n<h3 id=\"cc-implementation\"><span id=\"c-c-implementation\">C\/C++ Implementation<\/span><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">bool checkPalindrome(int original) {\n\n  int reverseNum = 0;\n  int tempOriginal = original;\n\n  while (tempOriginal > 0) {\n\n    int lastDigit = tempOriginal % 10;\n    reverseNum = reverseNum * 10 + lastDigit;\n    tempOriginal = tempOriginal \/ 10;\n  }\n\n  if (original == reverseNum) {\n    return true;\n  } else {\n    return false;\n  }\n}<\/pre>\n\n\n\n<h3 id=\"java-implementation\">Java Implementation<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public int checkPalindrome(int original) {\n  int reverseNum = 0;\n  int tempOriginal = original;\n\n  while (tempOriginal > 0) {\n\n    int lastDigit = tempOriginal % 10;\n    reverseNum = reverseNum * 10 + lastDigit;\n    tempOriginal = tempOriginal \/ 10;\n  }\n\n  if (original == reverseNum) {\n    return 1;\n  } else {\n    return 0;\n  }\n\n}<\/pre>\n\n\n\n<h3 id=\"python-implementation\">Python Implementation<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def checkPalindrome(original):\n    reverseNum = 0\n    tempOriginal = original\n\n    while (tempOriginal > 0):\n        lastDigit = tempOriginal % 10\n        reverseNum = reverseNum * 10 + lastDigit\n        tempOriginal = tempOriginal \/ 10\n\n    if (original == reverseNum):\n        return 1\n    else:\n        return 0<\/pre>\n\n\n\n<ul><li><strong>Time complexity:<\/strong> O(log10(N)), Where N is the given number.<\/li><li><strong>Space complexity:<\/strong> O(1)<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"special-case\">Special Case<\/h2>\n\n\n\n<p>The above solution will only work if the number is less than 10<sup>18<\/sup>, but what would happen if the number is greater than 10<sup>18<\/sup>?<\/p>\n\n\n\n<h2 id=\"implementation\">Implementation<\/h2>\n\n\n\n<h3 id=\"c-implementation-of-special-case\">C Implementation of Special Case<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">bool checkPalindrome(char original[]) {\n\n  int n = sizeof(original) \/ sizeof(original[0]);\n\n  for (int i = 0; i &lt; n \/ 2; i++) {\n\n    if (original[i] != original[n - 1 - i]) {\n      return false;\n    }\n  }\n\n  return true;\n}<\/pre>\n\n\n\n<h3 id=\"c-implementation-of-special-case\"><span id=\"c-implementation-of-special-case-2\">C++ Implementation of Special Case<\/span><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">bool checkPalindrome(string original) {\n\n  int n = original.size();\n\n  for (int i = 0; i &lt; n \/ 2; i++) {\n\n    if (original[i] != original[n - 1 - i]) {\n      return false;\n    }\n  }\n\n  return true;\n}<\/pre>\n\n\n\n<h3 id=\"java-implementation-of-special-case\">Java Implementation of Special Case<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public boolean checkPalindrome(String original) {\n\n  int n = original.length();\n\n  for (int i = 0; i &lt; n \/ 2; i++) {\n\n    if (original.charAt(i) != original.charAt(n - 1 - i)) {\n      return false;\n    }\n  }\n\n  return true;\n}<\/pre>\n\n\n\n<h3 id=\"python-implementation-of-special-case\">Python Implementation of Special Case<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def checkPalindrome(original):\n    \n    n = len(original)\n    for i in range(0, n\/\/2):\n        if original[i] != original[n - i - 1]:\n            return False\n           \n    return True<\/pre>\n\n\n\n<ul><li><strong>Time complexity:<\/strong> O(L), Where L is the length of the given string.<\/li><li><strong>Space complexity:<\/strong> O(1)<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"practice-question\">Practice Question<\/h2>\n\n\n\n<ul><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/problems\/palindrome-integer\/\" target=\"_blank\">Palindrome Integer<\/a><\/li><li><a href=\"https:\/\/www.interviewbit.com\/coding-interview-questions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Commonly Asked Coding Interview Questions<\/a><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-black-background-color has-black-color is-style-wide\"\/>\n\n\n\n<h2 id=\"frequently-asked-questions\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 id=\"q1-can-a-negative-number-be-palindrome\"><span id=\"q-1-can-a-negative-number-be-palindrome\">Q.1: Can a negative number be palindrome?<\/span><\/h3>\n\n\n\n<p id=\"can-a-negative-number-be-palindrome\"><strong>Ans:<\/strong> No, negative numbers can not be palindromes.<\/p>\n\n\n\n<h3 id=\"q2-can-we-solve-this-problem-without-converting-an-integer-to-a-string\"><span id=\"q-2-can-we-solve-this-problem-without-converting-an-integer-to-a-string\">Q.2: Can we solve this problem without converting an integer to a string?<\/span><\/h3>\n\n\n\n<p id=\"can-we-solve-this-problem-without-converting-integer-to-string\"><strong>Ans:<\/strong> Yes but only for integers less than 10<sup>18<\/sup>.<\/p>\n\n\n\n<h3 id=\"q3-are-all-single-digit-numbers-palindrome\"><span id=\"q-3-are-all-single-digit-numbers-palindrome\">Q.3: Are all single-digit numbers palindrome?<\/span><\/h3>\n\n\n\n<p id=\"q-are-all-single-digit-numbers-palindrome\"><strong>Ans:<\/strong> Yes<\/p>\n\n\n\n<h3 id=\"q4-how-is-the-time-complexity-of-the-above-algorithm-olog-10-n\"><span id=\"q-4-how-is-the-time-complexity-of-the-above-algorithm-olog10n\">Q.4: How is the time complexity of the above algorithm O(log<sub>10<\/sub>(N))?<\/span><\/h3>\n\n\n\n<p id=\"q-how-is-the-time-complexity-of-the-above-algorithm-olog-10-n\"><strong>Ans:<\/strong> Because we are dividing the input number N by 10 in every iteration. So, there is going to be a total of log<sub>10<\/sub>(N) iterations.<\/p>\n","protected":false},"excerpt":{"rendered":"A palindrome number is a number that remains the same when digits are reversed. For example, the number&hellip;\n","protected":false},"author":5,"featured_media":1998,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"1","csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0},"categories":[145],"tags":[194],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/1989"}],"collection":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/comments?post=1989"}],"version-history":[{"count":18,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/1989\/revisions"}],"predecessor-version":[{"id":17378,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/1989\/revisions\/17378"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/1998"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=1989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=1989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=1989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}