{"id":195751,"date":"2025-02-28T17:44:47","date_gmt":"2025-02-28T17:44:47","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=195751"},"modified":"2025-02-28T17:44:50","modified_gmt":"2025-02-28T17:44:50","slug":"exercises-what-is-recursion","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/28\/exercises-what-is-recursion\/","title":{"rendered":"Exercises What is Recursion"},"content":{"rendered":"\n<p>Exercises<\/p>\n\n\n\n<p>What is Recursion?<br>What is the base condition in recursion?<br>Why does a Stack Overflow error occur in recursion?<br>What is the importance of the stopping case in recursive functions?<br>Write a recursive function for the mathematical function:<br>f(n) = 1 if n = 1<br>f(n) = 2 * f(n-1) if n &gt;= 2<br>Write a function using Recursion to print numbers from n to 0.<br>What is the output for the following version of reverse()? void reverse() { int ch = getChar(); if (ch != &#8216;\\n&#8217;) reverse(); System.out.print(ch); }<br>Write a recursive method gcd(n, m) that returns the greatest common divisor of two integers n and m according to the following definition:<br>If m == 0, GCD(n, m) = n<br>If n mod m = 0, GCD(n, m) = m<br>Otherwise, GCD(n, m) = GCD(m, n mod m)<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The correct answer and explanation is:<\/strong><\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Recursion and Its Importance<\/strong><\/h3>\n\n\n\n<p>Recursion is a fundamental concept in computer science where a function calls itself to solve a problem. It breaks down a problem into smaller instances of the same problem until a base condition is met. Recursion is widely used in algorithms such as sorting (Merge Sort, Quick Sort), searching (Binary Search), and tree traversals.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Base Condition in Recursion<\/strong><\/h3>\n\n\n\n<p>The base condition is the stopping criterion that ensures a recursive function terminates. Without a base case, the recursion would continue indefinitely, leading to a stack overflow error.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why Does a Stack Overflow Error Occur in Recursion?<\/strong><\/h3>\n\n\n\n<p>A stack overflow error happens when a recursive function calls itself too many times without reaching the base condition. This leads to excessive memory usage, causing the program to crash.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Importance of the Stopping Case in Recursive Functions<\/strong><\/h3>\n\n\n\n<p>The stopping case, or base condition, is essential because it prevents infinite recursion and helps the function return results correctly. It also optimizes memory usage by limiting the number of recursive calls.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Recursive Function for f(n)<\/strong><\/h3>\n\n\n\n<p>Given the mathematical function:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>f(n)=1f(n) = 1 if n=1n = 1<\/li>\n\n\n\n<li>f(n)=2\u00d7f(n\u22121)f(n) = 2 \\times f(n-1) if n\u22652n \\geq 2<\/li>\n<\/ul>\n\n\n\n<p>The recursive function in Python is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def f(n):\n    if n == 1:\n        return 1\n    return 2 * f(n - 1)\n\nprint(f(5))  # Example Output: 16\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Recursive Function to Print Numbers from n to 0<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def countdown(n):\n    if n &lt; 0:\n        return\n    print(n)\n    countdown(n - 1)\n\ncountdown(5)\n<\/code><\/pre>\n\n\n\n<p>This function prints numbers from <code>n<\/code> down to <code>0<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output of the Given <code>reverse()<\/code> Function<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>void reverse() {\n    int ch = getChar();\n    if (ch != '\\n')\n        reverse();\n    System.out.print(ch);\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This function reads characters recursively until a newline <code>'\\n'<\/code> is encountered.<\/li>\n\n\n\n<li>Then, it prints characters in reverse order as the function unwinds.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Recursive GCD Function<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def gcd(n, m):\n    if m == 0:\n        return n\n    return gcd(m, n % m)\n\nprint(gcd(48, 18))  # Output: 6\n<\/code><\/pre>\n\n\n\n<p>This function follows the Euclidean algorithm to find the greatest common divisor.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Now, I will generate an image related to recursion.<\/p>\n\n\n\n<p>Here is the conceptual illustration of recursion, visually representing the idea of a self-referential structure. Let me know if you need any modifications or additional explanations!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/02\/image-1720.png\" alt=\"\" class=\"wp-image-195752\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Exercises What is Recursion?What is the base condition in recursion?Why does a Stack Overflow error occur in recursion?What is the importance of the stopping case in recursive functions?Write a recursive function for the mathematical function:f(n) = 1 if n = 1f(n) = 2 * f(n-1) if n &gt;= 2Write a function using Recursion to print [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-195751","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/195751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=195751"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/195751\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=195751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=195751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=195751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}