Create a process that will calculate the series 1 + x + x2 + x3 + x4 .......... + xn where x is a floating point number and n is a positive number. you process should be designed so exponentiation (or the pow function in c++) is not needed.

Respuesta :

Assumptions
   
1. n is an integer.
   
2. Language is c++
       
double expr(double x, int n)
   
{
   
double sum = 0.0;
   
double power = 1.0; // Will have the value x^y where y ranges from 0 to n
   
int i;
       
for(i=0; i <= n; ++i) {
   
sum += power;
   
power *= x;
   
}
   
return sum;
   
}