Logic Building
# Take n
n = int(input("Enter n: "))
# Calculate sum
sum_alt = 0
for i in range(1, n + 1):
if i % 2 == 1:
sum_alt += 1 / i
else:
sum_alt -= 1 / i
print(f"Sum: {sum_alt:.4f}")Output
Enter n: 5 Sum: 0.7833
Alternate signs based on position.
Key Concepts:
- Add for odd positions
- Subtract for even positions
- Create alternating pattern