Python Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n

This is a mathematical series application in which the user must enter the number of terms up to which the series’ sum is to be found. Following that, we need to know the value of x, which serves as the series’ starting point.

Given number of terms n and x the task is to calculate the sum of the given series in Python

Examples:

Example1:

Input:

total number of terms = 7       x=2

Output:

The total sum of the series with x= 2 and number of terms= 7 is 45.02

Example2:

Input:

Enter the total numbers of terms =17   x=1

Output:

The total sum of the series with x= 1 and number of terms= 17 is 3.44

Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n in Python

There are several ways to find the sum of the given series some of them are:

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Method #1:Using for loop (Static input)

Approach:

  • Give the number of terms and x  as static input.
  • Set a totalsum variable which calculates the total sum and initialize it to 1.
  • Calculate the numerator by using x**i where i is iterator value.
  • Find the total of the series using a for loop ranging from 2 to the given number of terms.
  • Print the totalsum of the series, rounded to two decimal places.
  • The Exit of the program.

Below is the implementation:

# Give the number of terms as static input.
numb = 7
# given value of x as static input
x = 2
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 1
# Find the total of the series using a for loop ranging from 1 to the number.
for value in range(2, numb+1):
    # Calculate the numerator by using x**i where i is iterator value.
    numer = x**value
    # calculating the total sum
    totalsum = totalsum+(numer/value)
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series with x=", x,
      'and number of terms=', numb, 'is', round(totalsum, 2))

Output:

The total sum of the series with x= 2 and number of terms= 7 is 45.02

Method #2:Using for loop (User input)

Approach:

  • Scan the total number of terms and x  as user input using int(input()).
  • Set a totalsum variable which calculates the total sum and initialize it to 1.
  • Calculate the numerator by using x**i where i is iterator value.
  • Find the total of the series using a for loop ranging from 2 to the given number of terms.
  • Print the totalsum of the series, rounded to two decimal places.
  • The Exit of the program.

Below is the implementation:

# Scan the total number of terms and x  as user input using int(input()).
numb = int(input("Enter the total numbers of terms ="))
# scan the x using int(input()) function
x = int(input('Enter given x = '))
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 1
# Find the total of the series using a for loop ranging from 1 to the number.
for value in range(2, numb+1):
    # Calculate the numerator by using x**i where i is iterator value.
    numer = x**value
    # calculating the total sum
    totalsum = totalsum+(numer/value)
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series with x=", x,
      'and number of terms=', numb, 'is', round(totalsum, 2))

Output:

Enter the total numbers of terms =6
Enter given x = 3
The total sum of the series with x= 3 and number of terms= 6 is 204.85

Method #3:Using while loop (User input)

Approach:

  • Scan the total number of terms and x  as user input using int(input()).
  • Set a totalsum variable which calculates the total sum and initialize it to 1.
  • Take a variable say value and initialize it to 2.
  • Using while loop calculate the total sum till value greater than given number
  • Calculate the numerator by using x**i where i is iterator value.
  • Calculate the totalsum by incrementing it value with numerator/value.
  • Increment the value by 1.
  • Print the totalsum of the series, rounded to two decimal places.
  • Exit of program.

Below is the implementation:

# Scan the total number of terms as user input by using int(input()) function.
numb = int(input("Enter the total numbers of terms ="))
# scan the x using int(input()) function
x = int(input('Enter given x = '))
# Take a variable say value and initialize it to 2.
value = 2
# Set a totalsum variable which calculates the total sum and initialize it to 1.
totalsum = 1
# Using while loop calculate the total sum till value greater than given number
while(value <= numb):
    # Calculate the numerator by using x**i where i is iterator value.
    numer = x**value
    # calculating the total sum
    totalsum = totalsum+(numer/value)
    # Increment the value by 1.
    value = value+1
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series with x=", x,
      'and number of terms=', numb, 'is', round(totalsum, 2))

Output:

Enter the total numbers of terms =17
Enter given x = 1
The total sum of the series with x= 1 and number of terms= 17 is 3.44

Method #4:Using While loop( Static Input)

Approach:

  • Give the number of terms and x  as static input.
  • Set a totalsum variable which calculates the total sum and initialize it to 1.
  • Take a variable say value and initialize it to 2.
  • Using while loop calculate the total sum till value greater than given number
  • Calculate the numerator by using x**i where i is iterator value.
  • Calculate the totalsum by incrementing it value with numerator/value.
  • Increment the value by 1.
  • Print the totalsum of the series, rounded to two decimal places.
  • Exit of program.

Below is the implementation:

# Give the number of terms as static input.
numb = 7
# given value of x as static input
x = 2
# Take a variable say value and initialize it to 2.
value = 2
# Set a totalsum variable which calculates the total sum and initialize it to 1.
totalsum = 1
# Using while loop calculate the total sum till value greater than given number
while(value <= numb):
    # Calculate the numerator by using x**i where i is iterator value.
    numer = x**value
    # calculating the total sum
    totalsum = totalsum+(numer/value)
    # Increment the value by 1.
    value = value+1
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series with x=", x,
      'and number of terms=', numb, 'is', round(totalsum, 2))

Output:

The total sum of the series with x= 2 and number of terms= 7 is 45.02

Related Programs: