Python Program to Find the Sum of Elements in a List using Recursion

This is a Python Program to find the sum of elements in a list recursively.

Problem Description

The program takes a list and finds the sum of elements in a list recursively.

Problem Solution

1. Define a recursive function which takes an array and the size of the array as arguments.
2. Declare an empty list and initialize it to an empty list.
3. Consider a for loop to accept values for the list.
4. Take the number of elements in the list and store it in a variable.
5. Accept the values into the list using another for loop and insert into the list.
6. Pass the list and the size of the list as arguments to the recursive function.
7. If the size of the list is zero, return 0.
8. Otherwise return the sum of the last element of the list along with the recursive function call (with the size reduced by 1).
9. The returned value is stored in a variable and the final sum is printed.
9. Exit.

Program/Source Code

Here is source code of the Python Program to find the sum of elements in a list recursively. The program output is also shown below.

def sum_arr(arr,size):
   if (size == 0):
     return 0
   else:
     return arr[size-1] + sum_arr(arr,size-1)
n=int(input("Enter the number of elements for list:"))
a=[]
for i in range(0,n):
    element=int(input("Enter element:"))
    a.append(element)
print("The list is:")
print(a)
print("Sum of items in list:")
b=sum_arr(a,n)
print(b)
Program Explanation

1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values to the same number of elements into the list.
3. The append function obtains each element from the user and adds the same to the end of the list as many times as the number of elements taken.
4. The list and the size of the list are passed as arguments to a recursive function.
5. If the size of the function reduces to 0, 0 is returned.
6. Otherwise the sum of the last element of the list along with the recursive function call (with the size reduced by 1) is returned.
7. The returned values are stored in a list and the sum of the elements in the list is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter the number of elements for list:3
Enter element:3
Enter element:56
Enter element:7
The list is:
[3, 56, 7]
Sum of items in list:
66
 
Case 2:
Enter the number of elements for list:5
Enter element:23
Enter element:45
Enter element:62
Enter element:10
Enter element:56
The list is:
[23, 45, 62, 10, 56]
Sum of items in list:
196

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.