{"id":6332,"date":"2025-06-02T16:59:04","date_gmt":"2025-06-02T11:29:04","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6332"},"modified":"2025-06-02T17:02:50","modified_gmt":"2025-06-02T11:32:50","slug":"print-numbers-from-n-to-1-using-recursion","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/print-numbers-from-n-to-1-using-recursion\/","title":{"rendered":"Print numbers from n to 1 using recursion"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n\n<style>\n .wp_blog_code-tabs-container {\n   font-family: \"Segoe UI\", sans-serif !important;\n   max-width: 900px !important;\n   margin: 2rem auto !important;\n   border: 1px solid #ddd !important;\n   border-radius: 8px !important;\n   overflow: hidden !important;\n   background-color: white !important;\n }\n\n\n .wp_blog_code-tabs-header {\n   background: #f7f7f7 !important;\n   display: flex !important;\n   border-bottom: 1px solid #ddd !important;\n }\n\n\n .wp_blog_code-tab-button {\n   flex: 1 !important;\n   padding: 10px 15px !important;\n   border: none !important;\n   background: transparent !important;\n   cursor: pointer !important;\n   font-weight: bold !important;\n   transition: background 0.2s !important;\n   color: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-button.active {\n   background: white !important;\n   border-bottom: 3px solid #0073aa !important;\n }\n\n\n .wp_blog_code-tab-content {\n   display: none !important;\n   padding: 20px !important;\n   background: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-content > pre {\n   background: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-content.active {\n   display: block !important;\n }\n\n\n .wp_blog_code-tab-content pre {\n   margin: 0 !important;\n   overflow-x: auto !important;\n }\n\n\n .wp_blog_explanation {\n   max-width: 900px !important;\n   margin: 2rem auto !important;\n   font-family: \"Segoe UI\", sans-serif !important;\n   line-height: 1.6 !important;\n   background: white !important;\n   color: black !important;\n   padding: 1rem !important;\n   border-radius: 8px !important;\n }\n\n\n .wp_blog_explanation h2 {\n   color: #0073aa !important;\n   font-size: 1.5rem !important;\n   margin-bottom: 0.5rem !important;\n }\n\n\n .wp_blog_explanation code {\n   background: #f1f1f1 !important;\n   padding: 2px 6px !important;\n   border-radius: 4px !important;\n   font-family: monospace !important;\n }\n<\/style>\n\n\n<!-- Explanation Block -->\n<div class=\"wp_blog_explanation\">\n <p>Recursion is a technique where a function calls itself to solve a problem by breaking it down into smaller sub-problems.<\/p>\n\n\n <h2>Base Condition<\/h2>\n <p>Every function call in recursion is stored in the call stack. If the recursion is too deep or has no base condition, the call stack keeps growing until memory is exhausted, causing a stack overflow error.<\/p>\n <p>A base condition is essential in recursion. It stops the recursion when a certain condition is met. Without it, recursion goes infinite and causes a stack overflow.<\/p>\n <pre><code>if (num === 0) return;<\/code><\/pre>\n\n\n <h2>Approach<\/h2>\n <p><strong>Problem:<\/strong> Print numbers from <code>n<\/code> to <code>1<\/code> using recursion.<\/p>\n <ul>\n   <li>Print the number.<\/li>\n   <li>Recurse with <code>num - 1<\/code>.<\/li>\n   <li>Stop when <code>num === 0<\/code>.<\/li>\n <\/ul>\n\n\n <h2>Time &#038; Space Complexity<\/h2>\n <ul>\n   <li><strong>Time Complexity:<\/strong> O(n) \u2013 one function call per number from n to 1.<\/li>\n   <li><strong>Space Complexity:<\/strong> O(n) \u2013 due to recursive call stack frames.<\/li>\n <\/ul>\n<\/div>\n\n\n<!-- Code Tabs -->\n<div class=\"wp_blog_code-tabs-container\">\n <div class=\"wp_blog_code-tabs-header\">\n   <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n   <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n   <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n   <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n   <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n   <button class=\"wp_blog_code-tab-button\" data-lang=\"cs\">C#<\/button>\n <\/div>\n\n\n <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n   <pre><code class=\"language-javascript\">\nfunction printDescending(num) {\n if (num === 0) return;\n console.log(num);\n printDescending(num - 1);\n}\n\n\nprintDescending(5);\n   <\/code><\/pre>\n <\/div>\n\n\n <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n   <pre><code class=\"language-c\">\n#include &lt;stdio.h&gt;\n\n\nvoid printDescending(int num) {\n if (num == 0) return;\n printf(\"%d\\n\", num);\n printDescending(num - 1);\n}\n\n\nint main() {\n printDescending(5);\n return 0;\n}\n   <\/code><\/pre>\n <\/div>\n\n\n <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n   <pre><code class=\"language-cpp\">\n#include &lt;iostream&gt;\nusing namespace std;\n\n\nvoid printDescending(int num) {\n if (num == 0) return;\n cout &lt;&lt; num &lt;&lt; endl;\n printDescending(num - 1);\n}\n\n\nint main() {\n printDescending(5);\n return 0;\n}\n   <\/code><\/pre>\n <\/div>\n\n\n <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n   <pre><code class=\"language-java\">\npublic class Main {\n static void printDescending(int num) {\n   if (num == 0) return;\n   System.out.println(num);\n   printDescending(num - 1);\n }\n\n\n public static void main(String[] args) {\n   printDescending(5);\n }\n}\n   <\/code><\/pre>\n <\/div>\n\n\n <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n   <pre><code class=\"language-python\">\ndef print_descending(num):\n   if num == 0:\n       return\n   print(num)\n   print_descending(num - 1)\n\n\nprint_descending(5)\n   <\/code><\/pre>\n <\/div>\n\n\n <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n   <pre><code class=\"language-csharp\">\nusing System;\n\n\nclass Program {\n static void PrintDescending(int num) {\n   if (num == 0) return;\n   Console.WriteLine(num);\n   PrintDescending(num - 1);\n }\n\n\n static void Main() {\n   PrintDescending(5);\n }\n}\n   <\/code><\/pre>\n <\/div>\n<\/div>\n\n\n<!-- Tab Switch Script -->\n<script>\n document.addEventListener('DOMContentLoaded', function () {\n   const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n   const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n\n\n   buttons.forEach(button => {\n     button.addEventListener('click', () => {\n       const lang = button.getAttribute('data-lang');\n\n\n       buttons.forEach(btn => btn.classList.remove('active'));\n       button.classList.add('active');\n\n\n       contents.forEach(content => {\n         content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n       });\n     });\n   });\n });\n<\/script>\n\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>Recursion is a technique where a function calls itself to solve a problem by breaking it down into smaller sub-problems. Base Condition Every function call in recursion is stored in the call stack. If the recursion is too deep or has no base condition, the call stack keeps growing until memory is exhausted, causing a<\/p>\n","protected":false},"author":1,"featured_media":0,"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,"footnotes":""},"categories":[260,811,810,174,172,173],"tags":[],"class_list":["post-6332","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6332"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6332\/revisions"}],"predecessor-version":[{"id":6333,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6332\/revisions\/6333"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}