Program to Find the Sum of the Series 1 + 12 + 13 + ….. + 1N

Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N

Given a number , the task is to find the sum of series 1 + 1 / 2 + …… + 1 /number in Python.

The inverse of a series is considered to be in Harmonic Progression if it follows the rule of an A.P, i.e. Arithmetic Progression. In general, the terms in a harmonic progression can be written as 1/a, 1/(a + d), 1/(a + 2d), 1/(a + 3d),…. 1/(a + nd).
As the Nth term of AP is (a + (n – 1)d).
As a result, the Nth term of harmonic progression is the reciprocal of the Nth term of AP, which is: 1/(a + (n – 1)d), where “a” is the first term of AP and “d” is the common difference.

Examples:

Example1:

Input:

total number of terms = 17

Output:

The total sum of the series  3.44

Example2:

Input:

Enter the total numbers of terms =26

Output:

The total sum of the series 3.85

Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N in Python

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

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Method #1:Using for loop (Static input)

Approach:

  • Give the number of terms as static input.
  • Set a totalsum variable which calculates the total sum and initialize it to 0.
  • Find the total of the series using a for loop ranging from 1 to the number.
  • 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 = 17
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 0
# Find the total of the series using a for loop ranging from 1 to the number.
for value in range(1, numb+1):
  # calculating the total sum
    totalsum = totalsum+(1/value)
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series ", round(totalsum, 2))

Output:

The total sum of the series  3.44

Method #2:Using for loop (User input)

Approach:

  • Scan the total number of terms as user input by using int(input()) function.
  • Set a totalsum variable which calculates the total sum and initialize it to 0.
  • Find the total of the series using a for loop ranging from 1 to the number.
  • 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 ="))
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 0
# Find the total of the series using a for loop ranging from 1 to the number.
for value in range(1, numb+1):
  # calculating the total sum
    totalsum = totalsum+(1/value)
# Print the totalsum of the series, rounded to two decimal places.
print("The total sum of the series ", round(totalsum, 2))

Output:

Enter the total numbers of terms =26
The total sum of the series 3.85

Method #3:Using while loop (User input)

Approach:

  • Scan the total number of terms as user input by using int(input()) function.
  • Set a totalsum variable which calculates the total sum and initialize it to 0.
  • Take a variable say value and initialize it to 1.
  • Using while loop calculate the total sum till value greater than given number
  • Calculate the totalsum by incrementing it value with 1/value.
  • Increment the value by 1.
  • 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 as user input by using int(input()) function.
numb = int(input("Enter the total numbers of terms ="))
# Take a variable say value and initialize it to 1.
value = 1
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 0
# Using while loop calculate the total sum till value greater than given number
while(value <= numb):
  # calculating the total sum
    totalsum = totalsum+(1/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 ", round(totalsum, 2))

Output:

Enter the total numbers of terms =26
The total sum of the series 3.85

Method #4:Using While loop( Static Input)

Approach:

  • Give the number of terms as static input.
  • Set a totalsum variable which calculates the total sum and initialize it to 0.
  • Take a variable say value and initialize it to 1.
  • Using while loop calculate the total sum till value greater than given number
  • Calculate the totalsum by incrementing it value with 1/value.
  • Increment the value by 1.
  • 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 = 26
# Take a variable say value and initialize it to 1.
value = 1
# Set a totalsum variable which calculates the total sum and initialize it to 0.
totalsum = 0
# Using while loop calculate the total sum till value greater than given number
while(value <= numb):
  # calculating the total sum
    totalsum = totalsum+(1/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 ", round(totalsum, 2))

Output:

The total sum of the series 3.85

Related Programs: