{"id":24255,"date":"2023-01-21T17:17:42","date_gmt":"2023-01-21T11:47:42","guid":{"rendered":"https:\/\/copyassignment.com\/?p=24255"},"modified":"2023-01-21T17:17:44","modified_gmt":"2023-01-21T11:47:44","slug":"python-program-for-the-n-th-fibonacci-number-using-recursion","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/python-program-for-the-n-th-fibonacci-number-using-recursion\/","title":{"rendered":"Python Program for the n-th Fibonacci number using recursion"},"content":{"rendered":"\n<p>Using recursion we have one way to solve this problem, let us take a look at it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1: Using multiple conditionals<\/h2>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"false\" data-copy=\"true\">\ndef get_nth_fibonacci(n):\n\tif n&lt;= 0:\n\t\tprint(\"Incorrect input\")\npython-program-f\n\telif n == 1:\n\t\treturn 0\n\n\telif n == 2:\n\t\treturn 1\n\telse:\n\t\treturn get_nth_fibonacci(n-1)+get_nth_fibonacci(n-2)\nn = int(input(\"Enter the n-th fibonacci number you want: \"))\nprint(\"The\",n,\"get_nth_fibonacci number is: \",get_nth_fibonacci(n))\n<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the n-th fibonacci number you want: 10\nThe 10 get_nth_fibonacci number is:  34<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>This is the most basic approach to solving this problem. We used conditional statements to check if the number is 0, if it is zero then we print the &#8220;Incorrect input&#8221;. In the same way, we defined conditions for 1 and 2 also. Now for the rest of the numbers, we used the function again and computed the value of n-1 and n-2, and finally added them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2: Using limited conditionals<\/h2>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><div style=\"position:absolute;top:-20px;right:0px;cursor:pointer\" class=\"copy-simple-code-block\"><span class=\"dashicon dashicons dashicons-admin-page\"><\/span><\/div><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"false\" data-copy=\"true\">\nfibo = [0, 1]\n\ndef get_nth_fibonacci(n):\n\tif n&lt;0:\n\t\tprint(\"Incorrect input\")\n\telif n&lt;= len(fibo):\n\t\treturn fibo[n-1]\n\telse:\n\t\ttemp_fib = get_nth_fibonacci(n-1)+get_nth_fibonacci(n-2)\n\t\tfibo.append(temp_fib)\n\t\treturn temp_fib\n\nprint(get_nth_fibonacci(n))\n\nn = int(input(\"Enter the n-th fibonacci number you want: \"))\nprint(\"The\",n,\"get_nth_fibonacci number is: \",get_nth_fibonacci(n))\n<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Enter the n-th fibonacci number you want: 10\nThe 10 get_nth_fibonacci number is:  34<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>The only difference between the previous method and this one is that we did not individually use the conditional for 1 and  2, instead we computed the length of the fibo list and simply returned the value that comes after computing <strong>fibo[n-1]<\/strong>. This will only be done if n is smaller than the length of the fibo.<\/p>\n\n\n\n<p>These above are the two methods using which we can solve the given problem.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/you-can-now-run-ai-fully-offline-on-your-phone-googles-gemma-4-just-changed-everything\/\">You Can Now Run AI Fully Offline on Your Phone \u2014 Google\u2019s Gemma 4 Just Changed Everything<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/i-built-a-24x7-ai-blogging-system-for-wordpress-using-python-free-full-code-inside\/\">I Built a 24\u00d77 AI Blogging System for WordPress Using Python (Free) \u2014 Full Code Inside<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/this-reddit-user-hacked-ai-with-simple-tricks-and-the-results-are-insane\/\">This Reddit User \u201cHacked\u201d AI With Simple Tricks\u2026 And The Results Are Insane<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/one-rm-rf-command-almost-wiped-out-100-million-worth-of-toy-story-2\/\">One \u201crm -rf\u201d Command Almost Wiped Out $100 Million Worth of Toy Story 2<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/how-to-make-money-with-chatgpt-in-2026-a-real-guide-that-still-works\/\">How to Make Money with ChatGPT in 2026: A Real Guide That Still Works<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/picoclaw-vs-openclaw-the-tiny-ai-that-could-replace-powerful-ai-agents\/\">PicoClaw vs OpenClaw: The Tiny AI That Could Replace Powerful AI Agents<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/oracle-layoffs-2026-people-woke-up-to-an-email-and-lost-their-jobs-instantly\/\">Oracle Layoffs 2026: People Woke Up to an Email\u2026 and Lost Their Jobs Instantly<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/x-new-video-update-is-breaking-a-basic-feature-and-users-are-not-happy\/\">X\u2019s New Video Update Is Breaking a Basic Feature \u2014 And Users Are Not Happy<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/the-most-shocking-military-tech-yet-robot-soldiers-that-could-change-warfare-forever\/\">The Most Shocking Military Tech Yet: Robot Soldiers That Could Change Warfare Forever<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sora-shutdown-the-reality-check-that-shook-ai-video-and-what-comes-next\/\">Sora Shutdown: The Reality Check That Shook AI Video \u2014 And What Comes Next<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/aya-expanse-supports-multiple-languages-for-diverse-global-applications\/\">Aya Expanse supports multiple languages for diverse global applications<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/alibaba-releases-page-agent-on-github-for-public-access\/\">Alibaba releases Page Agent on GitHub for public access<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/google-sheets-gemini-reaches-new-levels-of-performance-and-accuracy\/\">Google Sheets Gemini reaches new levels of performance and accuracy<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/artificial-intelligence-boosts-cardiac-care-in-rural-australian-communities\/\">Artificial intelligence boosts cardiac care in rural Australian communities<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/nvidia-gtc-2026-offers-insights-into-future-artificial-intelligence-developments\/\">NVIDIA GTC 2026 Offers Insights into Future Artificial Intelligence Developments<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/google-deepmind-updates-satellite-embedding-dataset-with-2025-data\/\">Google DeepMind Updates Satellite Embedding Dataset with 2025 Data<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/enhancing-hierarchical-instruction-in-advanced-large-language-models\/\">Enhancing hierarchical instruction in advanced large language models<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/meta-supports-community-development-near-its-data-centers-through-grants\/\">Meta supports community development near its data centers through grants<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/exploring-the-world-of-underwater-robotics-through-coding-techniques\/\">Exploring the world of underwater robotics through coding techniques<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/abb-robotics-partners-with-nvidia-for-large-scale-physical-ai-solutions\/\">ABB Robotics partners with NVIDIA for large scale physical AI solutions<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/why-all-ai-models-are-slowly-becoming-the-same-model\/\">Why All AI Models Are Slowly Becoming the Same Model<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/aam-aadmi-vs-corrupt-system-how-chatgpt-helped-one-guy-expose-govt-fraud-the-story-ravi-and-the-missing-light-pole\/\">Aam Aadmi vs Corrupt System: How ChatGPT Helped One Guy Expose Govt Fraud, The Story: \u201cRavi and The Missing Light Pole\u201d<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/chatgpt-asked-a-person-to-commit-suicide-to-solve-the-problem\/\">ChatGPT Asked a person to commit suicide to solve the problem<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/viral-moment-chinas-agibot-x2-makes-history-with-worlds-first-webster-backflip\/\">Viral Moment: China\u2019s AgiBot X2 Makes History With World\u2019s First Webster Backflip<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/terminator-rising-albania-hands-power-to-ai-echoing-a-nightmare-of-human-extinction\/\">Terminator Rising: Albania Hands Power to AI, Echoing a Nightmare of Human Extinction<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/what-is-albanias-world-first-ai-generated-minister-and-how-does-it-work\/\">What Is Albania\u2019s World-First AI-Generated Minister and How Does It Work?<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/does-chatgpt-believe-in-god-chatgpts-personal-opinion\/\">Does ChatGPT believe in God? ChatGPT&#8217;s Personal Opinion<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/chatgpt-vs-human-the-breath-holding-chat-that-ends-in-system-failure\/\">ChatGPT vs Human: The Breath-Holding Chat That Ends in \u201cSystem Failure\u201d<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/what-is-vibe-coding-the-future-of-no-code-programming-and-its-impact-on-software-developers\/\">What Is Vibe Coding? The Future of No-Code Programming and Its Impact on Software Developers<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/struggling-to-generate-ghibli-style-ai-images-heres-the-real-working-tool-that-others-wont-tell-you-about\/\">Struggling to Generate Ghibli-Style AI Images? Here\u2019s the Real Working Tool That Others Won\u2019t Tell You About!<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Using recursion we have one way to solve this problem, let us take a look at it. Method 1: Using multiple conditionals Output: Explanation: This&#8230;<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1930],"tags":[],"class_list":["post-24255","post","type-post","status-publish","format-standard","hentry","category-allcategorites","category-programs-in-python","wpcat-22-id","wpcat-1930-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/24255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=24255"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/24255\/revisions"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=24255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=24255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=24255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}