{"id":6336,"date":"2025-06-02T17:31:49","date_gmt":"2025-06-02T12:01:49","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6336"},"modified":"2025-06-02T17:31:49","modified_gmt":"2025-06-02T12:01:49","slug":"sum-of-all-numbers-in-array-using-recursion","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/sum-of-all-numbers-in-array-using-recursion\/","title":{"rendered":"Sum of all numbers in array 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<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  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\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  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-content > pre {\n    background: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\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  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\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<!-- Explanation Block -->\n<div class=\"wp_blog_explanation\">\n  <p>Write a function <code>sum(n)<\/code> that calculates the sum of all numbers in an array <code>arr<\/code> using recursion. It sums from index <code>0<\/code> to <code>n<\/code>.<\/p>\n\n  <h2>Concepts<\/h2>\n  <ul>\n    <li><strong>Recursion:<\/strong> The function keeps summing the element at index <code>n<\/code> and calls itself with <code>n-1<\/code>.<\/li>\n    <li><strong>Base Case:<\/strong> If <code>n == 0<\/code>, return the first element.<\/li>\n    <li><strong>Recursive Case:<\/strong> Return <code>arr[n] + sum(n - 1)<\/code>.<\/li>\n  <\/ul>\n\n  <h2>Time &#038; Space Complexity<\/h2>\n  <ul>\n    <li><strong>Time Complexity:<\/strong> <code>O(n)<\/code> \u2013 one recursive call per element.<\/li>\n    <li><strong>Space Complexity:<\/strong> <code>O(n)<\/code> \u2013 due to call stack.<\/li>\n  <\/ul>\n\n  <h2>Examples<\/h2>\n  <pre><code>Input: [5, 2, 6, 1, 3]\nOutput: 17\n\/\/ 5 + 2 + 6 + 1 + 3 = 17<\/code><\/pre>\n\n  <h2>Approach<\/h2>\n  <ul>\n    <li>If <code>n == 0<\/code>, return <code>arr[0]<\/code>.<\/li>\n    <li>Otherwise, return <code>arr[n] + sum(n - 1)<\/code>.<\/li>\n  <\/ul>\n<\/div>\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  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nlet arr = [5, 2, 6, 1, 3];\n\nfunction sum(n) {\n  if (n === 0) return arr[0];\n  return arr[n] + sum(n - 1);\n}\n\nconsole.log(sum(arr.length - 1)); \/\/ 17\n    <\/code><\/pre>\n  <\/div>\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\nint sum(int arr[], int n) {\n  if (n == 0) return arr[0];\n  return arr[n] + sum(arr, n - 1);\n}\n\nint main() {\n  int arr[] = {5, 2, 6, 1, 3};\n  int len = sizeof(arr) \/ sizeof(arr[0]);\n  printf(\"%d\\n\", sum(arr, len - 1)); \/\/ 17\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\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\nint sum(int arr[], int n) {\n  if (n == 0) return arr[0];\n  return arr[n] + sum(arr, n - 1);\n}\n\nint main() {\n  int arr[] = {5, 2, 6, 1, 3};\n  int len = sizeof(arr) \/ sizeof(arr[0]);\n  cout << sum(arr, len - 1) << endl; \/\/ 17\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\npublic class Main {\n  public static int sum(int[] arr, int n) {\n    if (n == 0) return arr[0];\n    return arr[n] + sum(arr, n - 1);\n  }\n\n  public static void main(String[] args) {\n    int[] arr = {5, 2, 6, 1, 3};\n    System.out.println(sum(arr, arr.length - 1)); \/\/ 17\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\ndef sum_array(arr, n):\n    if n == 0:\n        return arr[0]\n    return arr[n] + sum_array(arr, n - 1)\n\narr = [5, 2, 6, 1, 3]\nprint(sum_array(arr, len(arr) - 1))  # 17\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n    <pre><code class=\"language-csharp\">\nusing System;\n\nclass Program {\n    static int Sum(int[] arr, int n) {\n        if (n == 0) return arr[0];\n        return arr[n] + Sum(arr, n - 1);\n    }\n\n    static void Main() {\n        int[] arr = {5, 2, 6, 1, 3};\n        Console.WriteLine(Sum(arr, arr.Length - 1)); \/\/ 17\n    }\n}\n    <\/code><\/pre>\n  <\/div>\n<\/div>\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    buttons.forEach(button => {\n      button.addEventListener('click', () => {\n        const lang = button.getAttribute('data-lang');\n\n        buttons.forEach(btn => btn.classList.remove('active'));\n        button.classList.add('active');\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","protected":false},"excerpt":{"rendered":"<p>Write a function sum(n) that calculates the sum of all numbers in an array arr using recursion. It sums from index 0 to n. Concepts Recursion: The function keeps summing the element at index n and calls itself with n-1. Base Case: If n == 0, return the first element. Recursive Case: Return arr[n] +<\/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],"tags":[],"class_list":["post-6336","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6336","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=6336"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6336\/revisions"}],"predecessor-version":[{"id":6337,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6336\/revisions\/6337"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}