{"id":469,"date":"2024-01-04T09:44:15","date_gmt":"2024-01-04T09:44:15","guid":{"rendered":"https:\/\/www.kaashivinfotech.com\/blog\/?p=469"},"modified":"2025-07-16T08:28:14","modified_gmt":"2025-07-16T08:28:14","slug":"palindrome-number-in-c-java-and-python","status":"publish","type":"post","link":"https:\/\/www.kaashivinfotech.com\/blog\/palindrome-number-in-c-java-and-python\/","title":{"rendered":"Palindrome Number in C, Java, and Python"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>A palindrome number is a positive integer that remains the same when its digits are reversed. In other words, it reads the same forwards and backwards. For example, the numbers 121, 1331, and 4554 are palindrome numbers because they read the same regardless of the direction, while the numbers 123, 456, and 789 are not palindrome numbers.<\/p>\n<h2><strong>Problem Statement<\/strong><\/h2>\n<p>Problem Statement &#8211;\u00a0 Palindrome Number<\/p>\n<p>A palindrome number is a number that reads the same backward as forward. Given an integer, determine whether it is a palindrome or not.<\/p>\n<p>Write a program or function that takes an integer as input and returns true if it is a palindrome number, and false otherwise.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"size-medium wp-image-470 aligncenter\" src=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/10\/palindrom-300x295.png\" alt=\"\" width=\"300\" height=\"295\" srcset=\"https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/10\/palindrom-300x295.png 300w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/10\/palindrom-1024x1006.png 1024w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/10\/palindrom-768x754.png 768w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/10\/palindrom-1536x1509.png 1536w, https:\/\/www.kaashivinfotech.com\/blog\/wp-content\/uploads\/2023\/10\/palindrom.png 1911w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<h2><strong>Example<\/strong><\/h2>\n<p>1.Input &#8211;\u00a0 121<\/p>\n<p>Output &#8211;\u00a0 true (121 is a palindrome number)<\/p>\n<p>2.Input &#8211; 12321<\/p>\n<p>Output &#8211;\u00a0 true (12321 is a palindrome number)<\/p>\n<p>3.Input &#8211;\u00a0 12345<\/p>\n<p>Output &#8211;\u00a0 false (12345 is not a palindrome number)<\/p>\n<p>&nbsp;<\/p>\n<p>The formula to check if a positive integer is a palindrome involves reversing the digits of the number and comparing it with the original number. Here&#8217;s the formula in mathematical notation<\/p>\n<p>Let (n) be the original positive integer.<\/p>\n<p>Let (rev) be the number obtained by reversing the digits of (n).<\/p>\n<p>If (n = rev), then the integer (n) is a palindrome.<\/p>\n<p>Let&#8217;s go through an example using the number 121<\/p>\n<h2><strong>Example<\/strong><\/h2>\n<p>Original number &#8211; (n = 121)<\/p>\n<p>Reversed number &#8211; (rev = 121)<\/p>\n<p>Since (n = rev), the number 121 is a palindrome.<\/p>\n<p>Similarly, let&#8217;s take an example where the number is not a palindrome, like 123<\/p>\n<h2><strong>Example<\/strong><\/h2>\n<p>Original number &#8211;\u00a0 (n = 123)<\/p>\n<p>Reversed number &#8211;\u00a0 (rev = 321)<\/p>\n<p>Since (n \u2260 rev), the number 123 is not a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Palindromic_number\" target=\"_blank\" rel=\"noopener\">palindrome<\/a>.<\/p>\n<p>In code, this formula is implemented by extracting digits from the original number, reversing them, and then comparing the reversed number with the original number, as shown in the solutions provided earlier for C, Java, and Python.<\/p>\n<p>Let&#8217;s do a dry run of the palindrome check approach using the example number 12321.<\/p>\n<h2><strong>Example<\/strong><\/h2>\n<p>Number &#8211; 12321<\/p>\n<h3><strong>Step 1 &#8211;\u00a0 Initialize variables.<\/strong><\/h3>\n<p>num = 12321<\/p>\n<p>original = 12321<\/p>\n<p>reversed = 0<\/p>\n<h3><strong>Step 2 &#8211;\u00a0 Reverse the digits.<\/strong><\/h3>\n<p><u>Extract last digit &#8211;\u00a0 1<\/u><\/p>\n<p>reversed = (reversed * 10) + 1 = 0 + 1 = 1<\/p>\n<p>num = num \/\/ 10 = 1232<\/p>\n<p><u>Extract last digit &#8211; 2<\/u><\/p>\n<p>reversed = (reversed * 10) + 2 = 1 * 10 + 2 = 12<\/p>\n<p>num = num \/\/ 10 = 123<\/p>\n<p><u>Extract last digit &#8211; 3<\/u><\/p>\n<p>reversed = (reversed * 10) + 3 = 12 * 10 + 3 = 123<\/p>\n<p>num = num \/\/ 10 = 12<\/p>\n<p><u>Extract last digit &#8211; 2<\/u><\/p>\n<p>reversed = (reversed * 10) + 2 = 123 * 10 + 2 = 1232<\/p>\n<p>num = num \/\/ 10 = 1<\/p>\n<p><u>Extract last digit &#8211; 1<\/u><\/p>\n<p>reversed = (reversed * 10) + 1 = 1232 * 10 + 1 = 12321<\/p>\n<p>num = num \/\/ 10 = 0<\/p>\n<h3><strong>Step 3\u00a0<\/strong><\/h3>\n<p>Check if the reversed number is equal to the original number.<\/p>\n<h4><a href=\"https:\/\/www.kaashivinfotech.com\/python-course\/\"><strong>python<\/strong><\/a><\/h4>\n<p>original = 12321<\/p>\n<p>reversed = 12321<\/p>\n<p>Since original == reversed, the number 12321 is a palindrome.<\/p>\n<p>&nbsp;<\/p>\n<p>So, the final result is that the number 12321 is indeed a palindrome according to the palindrome check approach.<\/p>\n<h2><strong>Implementation<\/strong><\/h2>\n<h4><strong>C\/C++ Implementation<\/strong><\/h4>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">bool checkPalindrome(int original) {<br\/><br\/>int reverseNum = 0;<br\/><br\/>int tempOriginal = original;<br\/><br\/>while (tempOriginal &gt; 0) {<br\/><br\/>int lastDigit = tempOriginal % 10;<br\/><br\/>reverseNum = reverseNum * 10 + lastDigit;<br\/><br\/>tempOriginal = tempOriginal \/ 10;\u00a0 \/\/ Use integer division<br\/><br\/>}<br\/><br\/>if (original == reverseNum) {<br\/><br\/>return true;<br\/><br\/>} else {<br\/><br\/>return false;<br\/><br\/>}<br\/><br\/>}<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h4><strong>\u00a0<\/strong><strong>Java Implementation<\/strong><\/h4>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">public int checkPalindrome(int original) {<br\/><br\/>int reverseNum = 0;<br\/><br\/>int tempOriginal = original;<br\/><br\/>while (tempOriginal &gt; 0) {<br\/><br\/>int lastDigit = tempOriginal % 10;<br\/><br\/>reverseNum = reverseNum * 10 + lastDigit;<br\/><br\/>tempOriginal = tempOriginal \/ 10;<br\/><br\/>}<br\/><br\/>return original == reverseNum ? 1 : 0;<br\/><br\/>}<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h4><strong>\u00a0<\/strong><strong>Python Implementation<\/strong><\/h4>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">\u00a0def checkPalindrome(original):<br\/><br\/>reverseNum = 0<br\/><br\/>tempOriginal = original<br\/><br\/>while tempOriginal &gt; 0:<br\/><br\/>lastDigit = tempOriginal % 10<br\/><br\/>reverseNum = reverseNum * 10 + lastDigit<br\/><br\/>tempOriginal = tempOriginal \/\/ 10\u00a0 # Use integer division<br\/><br\/>if original == reverseNum:<br\/><br\/>return 1<br\/><br\/>else:<br\/><br\/>return 0<br\/><br\/>\u00a0Special Case<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>The above solution will only work if the number is less than 1018, but what would happen if the number is greater than 1018?<\/p>\n<p><strong>\u00a0<\/strong>Implementation<\/p>\n<h3><strong>C Implementation of Special Case<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdbool.h&gt;<br\/><br\/>#include &lt;string.h&gt;<br\/><br\/>bool checkPalindrome(char original[]) {<br\/><br\/>int n = strlen(original);<br\/><br\/>for (int i = 0; i &lt; n \/ 2; i++) {<br\/><br\/>if (original[i] != original[n - 1 - i]) {<br\/><br\/>return false;<br\/><br\/>}<br\/><br\/>}<br\/><br\/>return true;<br\/><br\/>}<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3><strong>\u00a0<\/strong><strong>C++ Implementation of Special Case<\/strong><\/h3>\n<p>C++ implementation of palindrome number detection that handles special cases like negative numbers and single-digit numbers:<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include &lt;string&gt;<br\/><br\/>bool checkPalindrome(std::string original) {<br\/><br\/>int n = original.size();<br\/><br\/>for (int i = 0; i &lt; n \/ 2; i++) {<br\/><br\/>if (original[i] != original[n - 1 - i]) {<br\/><br\/>return false;<br\/><br\/>}<br\/><br\/>}<br\/><br\/>return true;<br\/><br\/>}<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3><strong>Java Implementation of Special Case<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">public boolean checkPalindrome(String original) {<br\/><br\/>int n = original.length();<br\/><br\/>for (int i = 0; i &lt; n \/ 2; i++) {<br\/><br\/>if (original.charAt(i) != original.charAt(n - 1 - i)) {<br\/><br\/>return false;<br\/><br\/>}<br\/><br\/>}<br\/><br\/>return true;<br\/><br\/>}<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h3><strong>Python Implementation of Special Case<\/strong><\/h3>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">def checkPalindrome(original):<br\/><br\/>n = len(original)<br\/><br\/>for i in range(0, n\/\/2):<br\/><br\/>if original[i] != original[n - i - 1]:<br\/><br\/>return False<br\/><br\/>return True<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<h2><strong>Frequently Asked Questions<\/strong><\/h2>\n<h3><strong>1.What is a palindrome number?<\/strong><\/h3>\n<p>A palindrome number is a positive integer that remains the same when its digits are reversed. In other words, it reads the same forwards and backwards. For example, numbers like 121, 1331, and 4554 are palindrome numbers.<\/p>\n<h3><strong>2.How do you check if a number is a palindrome?<\/strong><\/h3>\n<p>To check if a number is a palindrome, you reverse its digits and compare the reversed number with the original number. If they are the same, the number is a palindrome.<\/p>\n<h3><strong>3.How do you handle negative numbers when checking for palindrome status?<\/strong><\/h3>\n<p>Negative numbers are typically not considered palindromes. You can include a check at the beginning of your code to return false if the input number is negative.<\/p>\n<h3><strong>4.Are single-digit numbers considered palindromes?<\/strong><\/h3>\n<p>Yes, by definition, single-digit numbers are considered palindromes because they read the same forwards and backwards.<\/p>\n<h3><strong>5.How do you handle special characters and spaces in a string palindrome check?<\/strong><\/h3>\n<p>When dealing with string palindromes, you usually remove spaces and ignore special characters. You can preprocess the string to remove spaces and non-alphanumeric characters before performing the palindrome check.<\/p>\n<h2><strong>Related Reads:<\/strong><\/h2>\n<ul>\n<li class=\"cs-entry__title\"><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/alphabet-in-numbers\/\">Alphabet in Numbers<\/a><\/li>\n<li>\n<p class=\"cs-entry__title\"><a href=\"https:\/\/www.kaashivinfotech.com\/blog\/optimize-your-seo-with-roman-numeral-slugs-for-clear-concise-and-keyword-rich-urls\/\">What is Roman Numbers ?<\/a><\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction A palindrome number is a positive integer that remains the same when its digits are reversed. In other words, it reads the same forwards and backwards. For example, the numbers 121, 1331, and 4554 are palindrome numbers because they read the same regardless of the direction, while the numbers 123, 456, and 789 are [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":8326,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2499],"tags":[2265,2266,2262,2264,2267,2268,2255,2260,2261,2253,2256,2254,2258,2259,2257,2263,772],"class_list":["post-469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","tag-check-a-number-is-palindrome-in-python","tag-check-a-number-is-palindrome-or-not-in-python","tag-check-number-is-palindrome-or-not-in-java","tag-find-palindrome-number-in-python","tag-number-palindrome","tag-numeric-palindrome-in-python","tag-palindrome","tag-palindrome-in-java","tag-palindrome-in-python","tag-palindrome-number","tag-palindrome-number-in-c","tag-palindrome-number-in-java","tag-palindrome-number-in-python","tag-palindrome-number-program-in-java","tag-palindrome-program-in-java","tag-palindrome-program-in-python","tag-python"],"_links":{"self":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=469"}],"version-history":[{"count":0,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/469\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media\/8326"}],"wp:attachment":[{"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaashivinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}