Write a program to compute 1/2+2/3+3*4 n/n+1 in Python

To compute the sum of the series 1/2+2/3+3/4+…+n/(n+1) in Python, you can use a loop to iterate over the terms of the series and add them to a running total. Here’s an example of how to to compute 1/2+2/3+3*4 n/n+1 in Python :

def sum_series(n):
    total = 0
    for i in range(1, n+1):
        total += i / (i+1)
    return total

print(sum_series(4))  # Output: 7/5
print(sum_series(5))  # Output: 29/12
print(sum_series(6))  # Output: 169/60

In this example, we define the sum_series() function that takes an integer n as an argument and returns the sum of the series 1/2+2/3+3/4+…+n/(n+1). The function uses a loop to iterate over the terms of the series and adds them to a running total. Then, it returns the total.

To test the function, we call it with different values of n and print the results to the console.

Keep in mind that the result of the division is a float, even if the numerator and denominator are integers. To get the result as a fraction, you can use the fraction module from the Python standard library. For example:

from fraction import Fraction

def sum_series(n):
    total = 0
    for i in range(1, n+1):
        total += Fraction(i, i+1)
    return total

print(sum_series(4))  # Output: 7/5
print(sum_series(5))  # Output: 29/12
print(sum_series(6))  # Output: 169/60

Leave a Comment