Sunday, 22 December 2024

Day 53: Python Program to Find Sum of Series 1+1/2+1/3...+1/N


 def sum_of_series(n):

    if n <= 0:

        return "N should be a positive integer."

    

    total_sum = 0

    for i in range(1, n + 1):

        total_sum += 1 / i

    return total_sum

try:

    N = int(input("Enter a positive integer N: "))

    if N <= 0:

        print("Please enter a positive integer.")

    else:

        result = sum_of_series(N)

        print(f"The sum of the series for N={N} is: {result:.6f}")

except ValueError:

    print("Invalid input! Please enter a positive integer.")

    

#source code --> clcoding.com 

Code Explanation:

1. Function Definition:

def sum_of_series(n):
    if n <= 0:
        return "N should be a positive integer."
sum_of_series(n):
This function takes an integer n as input, which represents the number of terms in the series.
If n is not a positive integer, it returns a message prompting the user to provide a valid input.

2. Series Calculation:
    total_sum = 0
    for i in range(1, n + 1):
        total_sum += 1 / i
total_sum = 0:
Initializes a variable to store the cumulative sum of the series.
for i in range(1, n + 1):
Iterates over the range from 1 to n (inclusive) to compute each term of the series.
total_sum += 1 / i:
Adds the reciprocal of the current number i (1+๐‘–) to the cumulative sum.

3. Return Statement:
    return total_sum
Returns the calculated sum of the series as a floating-point number.

4. Main Block (User Interaction):
try:
    N = int(input("Enter a positive integer N: "))
    if N <= 0:
        print("Please enter a positive integer.")
try-except Block:
Handles invalid input to ensure the program doesn't crash if the user enters non-integer values.
N = int(input(...)):
Prompts the user for input and converts it to an integer.
if N <= 0:
Checks if the user entered a positive integer. If not, a message is displayed.

5. Function Call and Output:
    else:
        result = sum_of_series(N)
        print(f"The sum of the series for N={N} is: {result:.6f}")
except ValueError:
    print("Invalid input! Please enter a positive integer.")
sum_of_series(N):
Calls the function with the user-provided value of N to compute the series sum.
Formatted Output ({result:.6f}):
Displays the result rounded to 6 decimal places for better readability.
except ValueError:
Catches non-integer inputs and prompts the user to enter a valid positive integer.

6. Example Execution:
Input:
Enter a positive integer N: 5
Execution:
The program calls sum_of_series(5).
The function calculates:
๐‘†=1+1/2+1/3+1/4+1/5=2.283333

The result is returned and printed.
Output:

The sum of the series for N=5 is: 2.283333


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (56) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (174) C (77) C# (12) C++ (82) Course (67) Coursera (228) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (60) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (935) Python Coding Challenge (368) Python Quiz (27) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses