Computer Science
Write a recursive function to add the first 'n' terms of the series:
1 + 1/2 - 1/3 + 1/4 - 1/5……………
Python
Python Functions
1 Like
Answer
def add_series_terms(n):
if n == 1:
return 1
elif n % 2 == 0:
return add_series_terms(n - 1) + 1 / n
else:
return add_series_terms(n - 1) - 1 / n
n = int(input("Enter the term: "))
print(add_series_terms(n))
Output
Enter the term: 2
1.5
Enter the term: 5
1.2166666666666668
Answered By
2 Likes
Related Questions
Write a program to display first four multiples of a number using recursion.
What do you understand by recursion ? State the advantages and disadvantages of using recursion.
Write a program to find the greatest common divisor between two numbers.
Write a Python function to multiply all the numbers in a list.
Sample List: (8, 2, 3, -1, 7)
Expected Output : -336