Program to Find Sum of All Array Elements Using Recursion
- Write a program to Find Sum of All Array Elements Using Recursion in C
- Write a program to Find Sum of All Array Elements Using Recursion in C++
- Write a program to Find Sum of All Array Elements Using Recursion in Python
- Write a program to Find Sum of All Array Elements Using Recursion in PHP
- Write a program to Find Sum of All Array Elements Using Recursion in Java
- Write a program to Find Sum of All Array Elements Using Recursion in Java Script
- Write a program to Find Sum of All Array Elements Using Recursion in C#
Explanation:
Recursion can be used to discover the total of all the elements in an array by breaking the array up into smaller pieces, adding the final element to the sum of the remaining components, and repeating the procedure until only one element is left.
Logic to Find Sum of All Array Elements Using Recursion
- Base Case:
- If the array has only one element, return that element.
- Recursive Case:
- Add the last element of the array to the sum of the rest of the array.
Recursive Algorithm:
- Define a function sum_array(array, n) where:
- array is the array of numbers.
- n is the size of the array or the current index being processed.
- Base Case:
- If n == 1, return array[0].
- Recursive Case:
- Compute sum_rest = sum_array(array, n – 1).
- Return array[n-1] + sum_rest
Program to Find Sum of All Array Elements Using Recursion
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int sumOfArray(int arr[], int n) { if (n == 0) { return 0; } return arr[n - 1] + sumOfArray(arr, n - 1); } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); printf("Sum of array elements = %d\n", sumOfArray(arr, n)); return 0; }
#include <iostream> using namespace std; int sumOfArray(int arr[], int n) { if (n == 0) { return 0; } return arr[n - 1] + sumOfArray(arr, n - 1); } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Sum of array elements = " << sumOfArray(arr, n) << endl; return 0; }
def sum_of_array(arr, n): if n == 0: return 0 return arr[n - 1] + sum_of_array(arr, n - 1) arr = [1, 2, 3, 4, 5] print("Sum of array elements =", sum_of_array(arr, len(arr)))
<?php function sumOfArray($arr, $n) { if ($n == 0) { return 0; } return $arr[$n - 1] + sumOfArray($arr, $n - 1); } $arr = array(1, 2, 3, 4, 5); $n = count($arr); echo "Sum of array elements = " . sumOfArray($arr, $n) . "\n"; ?>
public class Main { public static int sumOfArray(int[] arr, int n) { if (n == 0) { return 0; } return arr[n - 1] + sumOfArray(arr, n - 1); } public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; System.out.println("Sum of array elements = " + sumOfArray(arr, arr.length)); } }
function sumOfArray(arr, n) { if (n === 0) { return 0; } return arr[n - 1] + sumOfArray(arr, n - 1); } const arr = [1, 2, 3, 4, 5]; console.log("Sum of array elements =", sumOfArray(arr, arr.length));
using System; class Program { static int SumOfArray(int[] arr, int n) { if (n == 0) { return 0; } return arr[n - 1] + SumOfArray(arr, n - 1); } static void Main() { int[] arr = {1, 2, 3, 4, 5}; Console.WriteLine("Sum of array elements = " + SumOfArray(arr, arr.Length)); } }