Python Program to Find the Sum of Elements in a List Recursively














































Python Program to Find the Sum of Elements in a List Recursively



#Python Program to Find the Sum of Elements in a List Recursively """for calculating sum of a list recursively we need to call sum method recursively for that purpose we need to check for every element so time complexity for that algorithm is O(n) where 'n' is the size of the list Recursive call of sum function"""
#Program: def sum(arr, size): if(size == 0): return 0 else: return sum(arr, size-1) + arr[size-1] def main(): #Create a list list = [] #Taking maximum size of the list n = int(input("Enter the number of elements of the list: ")) print("Enter the element of the list: ") for i in range(0, n): t = int(input("")) list.append(t) #print the list print("Created list is: ") print(list) b = sum(list, i+1) print("Sum of the list is: ") print(b) main() """ Sample input: Enter the number of elements of the list: 7 Enter the element of the list: 5 8 9 15 6 12 3 Sample output: Created list is: [5, 8, 9, 15, 6, 12, 3] Sum of the list is: 58
"""

Comments

  • Saitharun
    9-May-2019 08:11:11 PM
    can you please explain how to use the code formatter