Series Sum Program Mathematical Logic
Python Code
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n (integer): "))
s = 0
for i in range(n + 1):
term = x ** i
s += term
print("Sum of the series is:", s)
Step-by-Step Explanation
1. Taking User Inputs:
x = float(input(...)) takes the base value (allowing decimals), while n = int(input(...)) receives the integer exponent limit.
2. Initializing Accumulator:
s = 0 sets up a running total variable to store the accumulated sum of the series.
3. The For Loop Setup:
for i in range(n + 1) iterates through power values from 0 up to and including n.
4. Calculating Term Power:
term = x ** i calculates the value of x raised to the power i (e.g., x⁰, x¹, x², …).
5. Accumulating Total:
s += term adds each evaluated power term directly to the running sum total.
6. Displaying Output:
print(...) outputs the final computed sum of the geometric series to the screen.
Worked Example: Input x = 2, n = 3
Series Formula: 1 + x + x² + x³ (since x⁰ = 1)
Initial State: x = 2, n = 3, s = 0
| Iteration (i) | Term (x ** i) | Calculation | s += term (Running Sum) |
|---|---|---|---|
| 0 | 2 ** 0 | 1 | 0 + 1 = 1 |
| 1 | 2 ** 1 | 2 | 1 + 2 = 3 |
| 2 | 2 ** 2 | 4 | 3 + 4 = 7 |
| 3 | 2 ** 3 | 8 | 7 + 8 = 15 |
Final Output: Sum of the series is: 15.0