

{"id":123237,"date":"2023-11-02T16:25:35","date_gmt":"2023-11-02T10:55:35","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123237"},"modified":"2024-02-28T11:34:14","modified_gmt":"2024-02-28T06:04:14","slug":"python-program-on-reverse-a-number-using-recursion","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/","title":{"rendered":"Python Program on Reverse a Number Using Recursion"},"content":{"rendered":"<p>In this article, we will explore a Python program that calculates the reverse of a number using recursion. The program highlights the use of recursion and a global variable to reverse the digits of a given number. Additionally, it checks if the reversed number is a palindrome, showcasing the versatility of recursive functions in solving more complex problems.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Basic understanding of functions.<\/li>\n<li>Familiarity with recursion.<\/li>\n<li>Knowledge of global variables in Python.<\/li>\n<li>Understanding of conditional statements in Python.<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<p>At its core, the program defines the reverse(n) function, which ingeniously employs recursion to calculate the reverse of a given number &#8216;n&#8217;. Notably, it introduces a global variable &#8216;s&#8217;, strategically utilized to store the reversed number. This article delves into the step-by-step workings of the code, illustrating how recursion plays a pivotal role in achieving the reversal.<\/p>\n<p>he program extends beyond basic reversal, incorporating an additional layer of complexity by examining whether the original and reversed numbers together form a palindrome. It invites user interaction by requesting input for the target number, then proceeds to call the reverse() function, displaying the reversed number. This two-pronged approach showcases not only the elegance of recursion but also the practicality of applying it to intricate problem-solving scenarios.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Reverse of a number using recursion\r\ns = 0\r\n\r\ndef reverse(n):\r\n    \"\"\"\r\n    Recursive function to calculate the reverse of a number.\r\n\r\n    Parameters:\r\n    - n (int): The input number.\r\n\r\n    Returns:\r\n    - int: The reversed number.\r\n    \"\"\"\r\n    global s\r\n    if n &gt; 0:\r\n        r = n % 10\r\n        s = s * 10 + r\r\n        n = n \/\/ 10\r\n        reverse(n)\r\n    return s\r\n\r\n# Calling the reverse function\r\nn = int(input(\"Enter a number: \"))\r\nx = reverse(n)\r\nprint(\"Reverse is %d\" % x)<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>Enter a number: 45<br \/>\nReverse is 54<\/p>\n<\/div>\n<h4>Code Explanation:<\/h4>\n<p><strong>1.<\/strong> Defines a global variable s initialized to 0<br \/>\n<strong>2.<\/strong> Defines a function reverse() that takes one integer parameter n<br \/>\n<strong>3. In the function:<\/strong><\/p>\n<p><strong>I)<\/strong> Base case: If n &lt;= 0, stop recurring<br \/>\n<strong>II)<\/strong> Get last digit: r = n % 10<br \/>\n<strong>III)<\/strong> Append r to s: s = s * 10 + r<\/p>\n<ul>\n<li>Builds the reverse number digit-by-digit<\/li>\n<\/ul>\n<p><strong>IV)<\/strong> <strong>Remove last digit:<\/strong> n = n \/\/ 10<br \/>\n<strong>V)<\/strong> Call reverse(n) again recursively<\/p>\n<p><strong>4.<\/strong> Return the global s which now contains the reversed number<br \/>\n<strong>5.<\/strong> Get user input for a number and store in n<br \/>\n<strong>6.<\/strong> Call reverse(n), store result in x<br \/>\n<strong>7.<\/strong> Print &#8220;Reverse is&#8221; along with the value of x<\/p>\n<h3>Summary<\/h3>\n<p>In conclusion, this Python program presents an instructive illustration of recursive techniques coupled with the use of a global variable to effectively reverse a user-provided number. It serves as a valuable educational resource for individuals with a foundational understanding of Python, offering a practical application of recursion and highlighting its versatility in problem-solving. By employing recursion to reverse the number and employing a global variable to store the result, the program provides a comprehensive learning experience, showcasing the elegance and efficiency of recursive algorithms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore a Python program that calculates the reverse of a number using recursion. The program highlights the use of recursion and a global variable to reverse the digits of&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[10333,28626,28651,28652,28653],"class_list":["post-123237","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-python-practical","tag-python-program-on-reverse-a-number-using-recursion","tag-reverse-a-number-using-recursion","tag-reverse-a-number-using-recursion-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on Reverse a Number Using Recursion - DataFlair<\/title>\n<meta name=\"description\" content=\"The program highlights the use of recursion and a global variable to reverse the digits of a given number.\" \/>\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\/python-program-on-reverse-a-number-using-recursion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on Reverse a Number Using Recursion - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The program highlights the use of recursion and a global variable to reverse the digits of a given number.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/\" \/>\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-11-02T10:55:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:04:14+00:00\" \/>\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=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on Reverse a Number Using Recursion - DataFlair","description":"The program highlights the use of recursion and a global variable to reverse the digits of a given number.","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\/python-program-on-reverse-a-number-using-recursion\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on Reverse a Number Using Recursion - DataFlair","og_description":"The program highlights the use of recursion and a global variable to reverse the digits of a given number.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-02T10:55:35+00:00","article_modified_time":"2024-02-28T06:04:14+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on Reverse a Number Using Recursion","datePublished":"2023-11-02T10:55:35+00:00","dateModified":"2024-02-28T06:04:14+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/"},"wordCount":402,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["Python","python practical","python program on reverse a number using recursion","reverse a number using recursion","reverse a number using recursion in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/","name":"Python Program on Reverse a Number Using Recursion - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-02T10:55:35+00:00","dateModified":"2024-02-28T06:04:14+00:00","description":"The program highlights the use of recursion and a global variable to reverse the digits of a given number.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-reverse-a-number-using-recursion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on Reverse a Number Using Recursion"}]},{"@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\/123237","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=123237"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123237\/revisions"}],"predecessor-version":[{"id":134125,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123237\/revisions\/134125"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}