C Online Compiler
Example: Sum of Array Elements in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Sum of Array Elements #include <stdio.h> int sumArray(int arr[], int size) { // Step 1: Define the base case // If the array is empty, its sum is 0. if (size <= 0) { return 0; } // Step 2: Define the recursive step // Sum = first element + sum of the rest of the array return arr[size - 1] + sumArray(arr, size - 1); } int main() { // Step 1: Initialize an array int numbers[] = {10, 20, 30, 40, 50}; // Step 2: Calculate the size of the array int size = sizeof(numbers) / sizeof(numbers[0]); // Step 3: Call the recursive sumArray function int total_sum = sumArray(numbers, size); // Step 4: Print the result printf("The sum of array elements is: %d\n", total_sum); return 0; }
Output
Clear
ADVERTISEMENTS