This Python program reads two numbers x and n from the user, and calculates the sum of the following expression:
x¹ + x² + x³ + ... + xⁿ
This type of calculation is common in mathematics, algorithm design, and numerical analysis.
# Read x and n from user
x = int(input("Enter value for x: "))
n = int(input("Enter value for n: "))
total = 0
# Calculate the sum of powers
for i in range(1, n + 1):
total += x ** i
print("Sum of powers from x¹ to xⁿ:", total)
Enter value for x: 2
Enter value for n: 4
Sum of powers from x¹ to xⁿ: 30
- x is the base, and n is the number of terms
- A loop runs from 1 to n, calculating x ** i at each step
- Each result is added to the total
- The final output is the sum of all powers from x¹ to xⁿ