I agree, it's better to do it yourself.
But, sometimes everyone needs help.
Here it is in C.
It's done twice, both ways should give the same answer.
Dan
// In C99, a single line comment, begins with "//".
// You need "stdio.h" for the function "printf", in the function "main".
#include <stdio.h>
// You need "math.h" for the function "pow", in the function "seriessum1".
#include <math.h>
// You need to list the two function prototypes.
double seriessum1(double x, int n);
double seriessum2(double x, int n);
int main(int argc, char *argv[])
{
double ss;
// Set the values of "x" and "n", here.
// "xm" means, "'x' in function 'main'".
// "nm" means, "'n' in function 'main'".
double xm = 3;
int nm = 8;
ss = seriessum1(xm, nm);
printf("series sum = %20.10f\n", ss);
ss = seriessum2(xm, nm);
printf("series sum = %20.10f\n", ss);
return 0;
}
double seriessum1(double x, int n)
{
int i, j;
double sum;
if(n < 1) return 0;
sum = 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= i; j++) sum += pow(x,(double)j);
return sum;
}
double seriessum2(double x, int n)
{
int i, j, k;
// "tsum" means, "temporary sum".
double sum, tsum;
if(n < 1) return 0;
sum = 0;
for(i = 1; i <= n; i++)
for(j = 1; j <= i; j++)
{
tsum = 1;
for(k = 1; k <= j; k++) tsum *= x;
sum += tsum;
}
return sum;
}
Bookmarks