

{"id":116560,"date":"2023-08-14T19:05:43","date_gmt":"2023-08-14T13:35:43","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=116560"},"modified":"2023-08-14T19:05:32","modified_gmt":"2023-08-14T13:35:32","slug":"palindrome-program-in-c","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/","title":{"rendered":"C Program to Check Whether a Number is Palindrome"},"content":{"rendered":"<p>A palindrome number reads the same backwards as forward. For example, 121 and 454 are palindrome numbers. In this article, we will explore how to write a C program to determine whether a given number is a palindrome or not. We will discuss the algorithm and provide a detailed step-by-step guide, along with code snippets, to help you understand and implement the solution.<\/p>\n<h3>Algorithm to check Palindrome using C:<\/h3>\n<p>To check whether a number is a palindrome, we need to compare the digits of the number in a symmetric manner. Here&#8217;s the algorithm we&#8217;ll follow:<\/p>\n<p>1. Accept the input number from the user.<\/p>\n<p>2. Copy the input number to a separate variable for comparison.<\/p>\n<p>3. Initialize a variable to store the reverse of the number (initially set to 0).<\/p>\n<p>4. Extract the last digit of the number using the modulus operator (%).<\/p>\n<p>5. Multiply the reverse variable by ten and add the extracted digit to it.<\/p>\n<p>6. Divide the original number by 10 to remove the last digit.<\/p>\n<p>7. Repeat steps 4-6 until the original number becomes 0.<\/p>\n<p>8. Compare the reverse variable with the initially copied number.<\/p>\n<p>9. If they are equal, the number is a palindrome; otherwise, it is not.<\/p>\n<h3>C Program to Check Whether a Number is a Palindrome:<\/h3>\n<p>Now let&#8217;s dive into the C code that implements the algorithm explained above:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\nint main() {\r\n    int number, reverse = 0, original, remainder;\r\n    printf(\"Enter a number: \");\r\n    scanf(\"%d\", &amp;number);\r\n    original = number;\r\n \r\n    while(number!=0){\r\n        remainder = number%10;\r\n        reverse = reverse*10 + remainder;\r\n        number = number\/10;\r\n    }\r\n\r\nif (original == reverse) {\r\n    printf(\"%d is a palindrome number.\\n\", original); \r\n}   \r\nelse {\r\n    printf(\"%d is not a palindrome number.\\n\", original);\r\n}\r\nreturn 0;\r\n}\r\n<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>1. We start by accepting the input number from the user using the `scanf()` function.<\/p>\n<p>2. Then, create variables `reverse`, `original`, and `remainder` to store the reversed number, the original number, and the remainder of each digit, respectively.<\/p>\n<p>3. We copy the original number to the `original` variable for later comparison.<\/p>\n<p>4. Using a `while` loop, we extract the last digit of the number by calculating the remainder with 10.<\/p>\n<p>5. We update the `reverse` variable by multiplying it by ten and adding the extracted digit.<\/p>\n<p>6. We divide the number by 10 to remove the last digit.<\/p>\n<p>7. The loop continues until the original number becomes 0.<\/p>\n<p>8. Finally, we compare the `original` and `reverse` variables to determine whether the number is a palindrome or not. The result is displayed using `printf()`.<\/p>\n<h3>Alternative methods to check palindrome using C Programming Language:<\/h3>\n<p><strong>1<\/strong>\u00a0<strong>Using an Array<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\nint main() {\r\nint number,dataflair, reverse = 0, i, digit[10];\r\nprintf(\"Enter a number: \");\r\nscanf(\"%d\", &amp;number);\r\ndataflair = number;\r\nfor (i = 0; number != 0; i++) {\r\n}\r\ndigit[i] = number % 10;\r\nnumber = number \/ 10;\r\nfor (int j = 0; j &lt; i; j++) {\r\n}\r\nreverse = reverse 10 + digit[j];\r\n}\r\nif (dataflair == reverse) {\r\nprintf(\"%d is a palindrome number.\\n\", dataflair); } else {\r\nprintf(\"%d is not a palindrome number.\\n\", dataflair);}\r\nreturn 0;\r\n}<\/pre>\n<p><strong>2<\/strong>\u00a0<strong>C Program to check Palindrome Using Recursion<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\nint ispalindrome (int dataflair, int original) {\r\nif (dataflair == 0) {\r\nreturn original;\r\n} else {\r\n}\r\nreturn ispalindrome (dataflair \/ 10, original * 10 + number % 10);\r\n}\r\nint main() {\r\nint dataflair;\r\nprintf(\"Enter a number: \");\r\nscanf(\"%d\", &amp;dataflair);\r\nint result = isPalindrome (dataflair, 0);\r\nif (dataflair == result) {\r\nprintf(\"%d is a palindrome number.\\n\", dataflair); } else {\r\nprintf(\"%d is not a palindrome number.\\n\", dataflair);}\r\nreturn 0;\r\n}\r\n<\/pre>\n<p><strong>3 Using a String to find palindrome in C<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt; \r\n#include &lt;string.h&gt;\r\nint main() {\r\nchar str[20];\r\nprintf(\"Enter a number: \"); scanf(\"%s\", dataflair);\r\nint length = strlen(dataflair); int ispalindrome = 1;\r\nfor (int i = 0; i &lt; length \/ 2; i++) { if (dataflair[i] != dataflair[length - i - 1]) { ispalindrome = 0;\r\nbreak;\r\n}\r\n}\r\nif (isPalindrome) {\r\nprintf(\"%s is a palindrome number.\\n\", dataflair);\r\n} else {\r\nprintf(\"%s is not a palindrome number.\\n\", dataflair);\r\n}\r\nreturn 0;\r\n<\/pre>\n<p>These alternative methods demonstrate different approaches to solving the palindrome number problem in C, while still achieving the same result.<\/p>\n<p><b>Output<\/b><\/p>\n<div class=\"code-output\">Enter a number: 126789<br \/>\n126789 is not a palindrome number.<br \/>\nEnter a number: 96969<br \/>\n96969 is a palindrome number.<\/div>\n<h3>Conclusion<\/h3>\n<p>By following the step-by-step guide and examining the code snippets provided, you should now have a clear understanding of how to write a C program to check whether a number is a palindrome. Palindrome numbers are a fascinating concept, and implementing this program showcases how logical thinking and basic programming constructs can be utilized to solve interesting problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A palindrome number reads the same backwards as forward. For example, 121 and 454 are palindrome numbers. In this article, we will explore how to write a C program to determine whether a given&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":117866,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19488],"tags":[20194,27996],"class_list":["post-116560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-palindrome-in-c","tag-palindrome-program-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C Program to Check Whether a Number is Palindrome - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn how to write C program to check whether a number is a palindrome. See various ways like using arrays, using recursion and using string.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C Program to Check Whether a Number is Palindrome - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn how to write C program to check whether a number is a palindrome. See various ways like using arrays, using recursion and using string.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-14T13:35:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-check-whether-a-number-is-palindrome.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C Program to Check Whether a Number is Palindrome - DataFlair","description":"Learn how to write C program to check whether a number is a palindrome. See various ways like using arrays, using recursion and using string.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/","og_locale":"en_US","og_type":"article","og_title":"C Program to Check Whether a Number is Palindrome - DataFlair","og_description":"Learn how to write C program to check whether a number is a palindrome. See various ways like using arrays, using recursion and using string.","og_url":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-08-14T13:35:43+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-check-whether-a-number-is-palindrome.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"C Program to Check Whether a Number is Palindrome","datePublished":"2023-08-14T13:35:43+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/"},"wordCount":467,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-check-whether-a-number-is-palindrome.webp","keywords":["Palindrome in C","palindrome program in c"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/","url":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/","name":"C Program to Check Whether a Number is Palindrome - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-check-whether-a-number-is-palindrome.webp","datePublished":"2023-08-14T13:35:43+00:00","description":"Learn how to write C program to check whether a number is a palindrome. See various ways like using arrays, using recursion and using string.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-check-whether-a-number-is-palindrome.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-check-whether-a-number-is-palindrome.webp","width":1200,"height":628,"caption":"c program to check whether a number is palindrome"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/palindrome-program-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"C Program to Check Whether a Number is Palindrome"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=116560"}],"version-history":[{"count":8,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116560\/revisions"}],"predecessor-version":[{"id":118040,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116560\/revisions\/118040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/117866"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=116560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=116560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=116560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}