This Python program defines a 10-element array with initial values and calculates the sum of its elements using recursion.
The recursive function adds one element at a time to the sum of the remaining elements until the end of the array is reached.
def recursive_sum(arr: list[int], index: int = 0) -> int:
if index == len(arr):
return 0
return arr[index] + recursive_sum(arr, index + 1)
# Define initial array
array = [3, 7, 2, 9, 5, 1, 4, 6, 8, 10]
# Compute sum recursively
total = recursive_sum(array)
# Display result
print("Array:", array)
print("Recursive sum:", total)
Array: [3, 7, 2, 9, 5, 1, 4, 6, 8, 10]
Recursive sum: 65