This Python program reads an integer n from the user and calculates the first n terms of a mathematical series.
In this example, we use the arithmetic series:
S = 1 + 2 + 3 + ... + n
Each term is printed along with the cumulative sum.
def calculate_series(n):
total = 0
for i in range(1, n + 1):
total += i
print(f"Term {i}: {i}, Cumulative Sum: {total}")
# Run the program
n = int(input("Enter number of terms: "))
calculate_series(n)
Term 1: 1, Cumulative Sum: 1
Term 2: 2, Cumulative Sum: 3
Term 3: 3, Cumulative Sum: 6
Term 4: 4, Cumulative Sum: 10
Term 5: 5, Cumulative Sum: 15
- The program first reads the number of terms n from the user
- A for loop iterates from 1 to n
- Each term is added to a cumulative total and printed
- The output includes both the individual term and the running sum