Part of the series

Several example codes

~1 min read • Updated Oct 5, 2025

Program Overview

This Python program reads three integers x, n, and k from the user, and computes the value of the expression:
\[ F(x,n,k) = \frac{x^2 + x^3 + \dots + x^n}{k} \]
The numerator is the sum of powers of x from 2 to n, and the denominator is simply k.


Python Code:


# Read x, n, and k from user
x = int(input("Enter value for x: "))
n = int(input("Enter value for n: "))
k = int(input("Enter value for k: "))

# Compute numerator: sum of x^2 to x^n
numerator = 0
for j in range(2, n + 1):
    numerator += x ** j

# Compute denominator: sum of 1 repeated k times = k
denominator = k if k != 0 else 1  # Avoid division by zero

# Final result
F = numerator / denominator
print("F(x, n, k) =", F)

Sample Output:


Enter value for x: 2  
Enter value for n: 4  
Enter value for k: 3  
F(x, n, k) = 6.0

Step-by-Step Explanation:

- The loop starts from 2 to n to exclude from the numerator
- The denominator is simply k, since ∑ₖ₌₁¹ = k
- A check is added to avoid division by zero if k = 0
- The final result is printed as a floating-point value


Written & researched by Dr. Shahin Siami