WAP in C++ & Python to Find the Sum of Series 1+x+x^2+……+x^n

By | September 7, 2019

Here in this program, we will code to find the sum of a special series which is 1+x+x^2+……+x^n. Here x is the variable and n will represent up to which power the sum will continue.

 C++ Program to find the Sum of series 1+x+x^2+……+x^n

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
long float sum=1,x,n,i;
cout<<"Here we will calculate the sum of series 1+ x + x^2+ ...+ x^n \n";
cout<<"Enter the value of x :";
cin>>x;
cout<<"Enter the value of n: ";
cin>>n;
for(i=1;i<=n;i++)
    {
    sum += pow(x,i);
    }
cout<<"The sum of the series is: "<<sum;
getch();
}

Output:

Here we will calculate the sum of series 1 +x +x^2 +……+ x^n

Enter the value of x: 5
Enter the value of n: 7
The sum of the series is: 97656

Python Program to find the sum of series 1+x+x^2+……+x^n

import math
sum=1
print("Here we will calculate the sum of series 1 +x +x^2 +……+ x^n")
x=int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
for i in range(1,n+1):
    sum+=math.pow(x,i)
print("The sum of the series is:", int(sum))

Output:

Here we will calculate the sum of series 1 +x +x^2 +……+ x^n

Enter the value of x: 3
Enter the value of n: 4
The sum of the series is: 121

Leave a Reply

Your email address will not be published. Required fields are marked *