Write a program to input the value of x and n and print the sum of the following series: 1) 1 + x² + x³ + … + xⁿ

Here,We are going to write python program to input the value of x and n and print the sum of series :1 + x² + x³ + … + xⁿ. We can print the sum of series by using if-else loop or we can use for loop to print the sum of above series.

Python program to print Series 1 + x + x^2 + X^3 + …. using if-else loop

#Program to print 1) 1 + x² + x³ + … + xⁿ

x = int(input("Enter the value of x : "))
n = int(input("Enter the value of n : "))
sum = 0
for i in range(n+1):
    if(i%2 == 0):
        sum = sum + (x**i)
    else:
        sum = sum + (x**i)


print("Sum of Series is :",sum)



Output

Enter the value of x : 2
Enter the value of n : 5
Sum of Series is : 63

Python program to print series using for loop

#Program to print 1 + x + x2 + x3 + x4 ...............
x = int(input("Enter the value of x : "))
n = int(input("Enter the value of n : "))
sum = 0
for i in range(n+1):
    sum = sum +(x**i)
print("Sum of Series is :",sum)


Output

Enter the value of x : 3
Enter the value of n : 10
Sum of Series is : 88573
<