~1 min read • Updated Oct 5, 2025
Program Overview
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.
Python Code:
# 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)
Sample Output:
Enter value for x: 2
Enter value for n: 4
Sum of powers from x¹ to xⁿ: 30
Step-by-Step Explanation:
- 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ⁿ
Written & researched by Dr. Shahin Siami