Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sum_array_elements_using_recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Recursive function to sum elements of an array in Python

#Function Definiton
def sum_array(ary):
# Base case: if the array is empty, the sum is 0
if len(ary) == 0:
return 0
# Recursive case: add the first element to the sum of the rest of the array
else:
return ary[0] + sum_array(ary[1:])

# Example:
my_array = [11, 31, 55, 72, 98 , 10, 13, 51, 71, 91, 99, 745, 429, 658]
result = sum_array(my_array)
print("The sum of the array is:", result)