18. Program to calculate and display sum of the following series using for loop: x + x^2 + x^3 + ... x^n

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 18:

Write a program to calculate and display sum of the following series using for loop:
x + x^2 + x^3 + ... x^n

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
#include<math.h>
int main()
{
    int x, n, num, sum=0;
    cout<<"Enter the value of x:";
    cin>>x;
    cout<<"\nEnter the value of n:";
    cin>>n;
    for(num=1;num<=n;num++)
    {
        sum=sum+pow(x,num);
    }
    cout<<"\nSum of the series is:"<<sum;      
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
17. Program that takes n numbers as input. It displays total positive and negative numbers.

Comments