C Online Compiler
Example: Array Sum using Recursion in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Array Sum using Recursion #include <stdio.h> // Recursive function to sum array elements // arr: pointer to the current start of the sub-array // n: number of elements remaining in the sub-array int sumArrayRecursive(int *arr, int n) { // Step 1: Base Case - If no elements are left, the sum is 0 if (n <= 0) { return 0; } // Step 2: Recursive Step - Add the first element to the sum of the rest of the array return arr[0] + sumArrayRecursive(arr + 1, n - 1); } int main() { int arr[] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); int sum = sumArrayRecursive(arr, n); printf("The sum of array elements using recursion is: %d\n", sum); return 0; }
Output
Clear
ADVERTISEMENTS