Monday, June 29, 2020

Write a Python program to calculate the sum of the positive integers of n+(n-2)+(n-4)... (until n-x =< 0).

def sum_series(n): if n < 1: return 0 else: return n + sum_series(n - 2) print(sum_series(6)) print(sum_series(10))

Sample Output:

12                                                                                                            
30
Flowchart: Recursion: Sum of  n+(n-2)+(n-4)....
sum_series(6)=6 + sum_series(4)
sum_series(6)=6 + 4+sum_series(2)
sum_series(6)=6 + 4+sum_series(2)
sum_series(6)=6 + 4+2+0

No comments:

Post a Comment