Skip to content

Commit 48e857d

Browse files
quubbletechyminati
authored andcommitted
Program to sum_array_elements_using_recursion in Python
1 parent e5510d1 commit 48e857d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Recursive function to sum elements of an array in Python
2+
3+
#Function Definiton
4+
def sum_array(ary):
5+
# Base case: if the array is empty, the sum is 0
6+
if len(ary) == 0:
7+
return 0
8+
# Recursive case: add the first element to the sum of the rest of the array
9+
else:
10+
return ary[0] + sum_array(ary[1:])
11+
12+
# Example:
13+
my_array = [11, 31, 55, 72, 98 , 11, 13, 51, 71, 91, 99, 745, 429, 658]
14+
result = sum_array(my_array)
15+
print("The sum of the array is:", result)
16+

0 commit comments

Comments
 (0)