Python Program to Find Sum of Series 1/2-2/3+3/4-4/5+5/6…+N/N+1

In the previous article, we have discussed Python Program to Find Sum of Series (1+(1*2)+(1*2*3)+…till N)

Given a number N and the task is to find the sum of the given series (1/2-2/3+3/4-4/5+5/6…+N/N+1) for the given number in Python.

Examples:

Example1:

Input:

Given Number = 17

Output:

The total sum of the series till the given number { 17 } =  0.6661398242280596

Example2:

Input:

Given Number = 6

Output:

The total sum of the series till the given number { 6 } =  -0.2404761904761904

Program to Find Sum of Series 1/2-2/3+3/4-4/5+5/6…+N/N+1 in Python

Below are the ways to find the sum of the given series (1/2-2/3+3/4-4/5+5/6…+N/N+1) for the given number in Python :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.0 (floating number).
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Check if the value k modulus 2 is even or not using the if conditional statement.
  • If the statement is True, then subtract k/(k+1) from the above-declared resltsum.
  • Store it in the same variable resltsum.
  • Else if the statement is False, then add k/(k+1) to the above-declared resltsum.
  • Store it in the same variable resltsum.
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 17
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.0 (floating number).
resltsum = 0.0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
    # Check if the value k modulus 2 is even or not using the if conditional statement.
    if(k % 2 == 0):
        # If the statement is True, then subtract k/(k+1) from the above-declared resltsum.
        # Store it in the same variable resltsum.
        resltsum -= k/(k+1)
    else:
      # Else if the statement is False, then add k/(k+1) to the above-declared resltsum.
       # Store it in the same variable resltsum.
        resltsum += k/(k+1)
 # Increment the value of k by 1 and store it in the same variable k.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 17 } =  0.6661398242280596

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.0 (floating number).
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Check if the value k modulus 2 is even or not using the if conditional statement.
  • If the statement is True, then subtract k/(k+1) from the above-declared resltsum.
  • Store it in the same variable resltsum.
  • Else if the statement is False, then add k/(k+1) to the above-declared resltsum.
  • Store it in the same variable resltsum.
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.0 (floating number).
resltsum = 0.0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
    # Check if the value k modulus 2 is even or not using the if conditional statement.
    if(k % 2 == 0):
        # If the statement is True, then subtract k/(k+1) from the above-declared resltsum.
        # Store it in the same variable resltsum.
        resltsum -= k/(k+1)
    else:
      # Else if the statement is False, then add k/(k+1) to the above-declared resltsum.
       # Store it in the same variable resltsum.
        resltsum += k/(k+1)
 # Increment the value of k by 1 and store it in the same variable k.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some Random Number = 6
The total sum of the series till the given number { 6 } = -0.2404761904761904

Practice Python Program Examples to master coding skills and learn the fundamental concepts in the dynamic programming language Python.