In the previous article, we have discussed Python Program to Find the Sum of Series 3+7+13+21…..+N
Given a number N and the task is to find the sum of the given series (1+1/3+1/5+1/7+…..1/(N+2)) for the given number in Python.
Examples:
Example1:
Input:
Given Number = 25
Output:
The total sum of the series till the given number { 25 } = 2.2643528386481675Example2:
Input:
Given Number = 14
Output:
The total sum of the series till the given number { 14 } = 1.9551337551337549Program to Find Sum of Series 1+1/3+1/5+1/7+…..1/(N+2) in Python
Below are the ways to find the sum of the given series (1+1/3+1/5+1/7+…..1/(N+2)) 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 (to get the floating-point 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.
- Inside the loop, Add 1 divided by k to the above-initialized resltsum and store it in the same variable resltsum.
- Increment the value of k by 2 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 = 25
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.0 (to get the floating-point 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):
# Inside the loop, Add 1 divided by k to the above-initialized resltsum and
# store it in the same variable resltsum.
resltsum += 1/k
# Increment the value of k by 2 and store it in the same variable k.
k += 2
# 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 { 25 } = 2.2643528386481675Method #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 (to get the floating-point 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.
- Inside the loop, Add 1 divided by k to the above-initialized resltsum and store it in the same variable resltsum.
- Increment the value of k by 2 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 (to get the floating-point 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):
# Inside the loop, Add 1 divided by k to the above-initialized resltsum and
# store it in the same variable resltsum.
resltsum += 1/k
# Increment the value of k by 2 and store it in the same variable k.
k += 2
# 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 = 14
The total sum of the series till the given number { 14 } = 1.9551337551337549Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.
