0

thank for the help, I'm new to c programming.

So is this:

```
#include <stdio.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
int main()
{
int k, i;
double sum=0;
printf("Enter number of terms: ");
scanf("%d",&k);
for (i=1; i<=k; i++)
{
if (i%2==0)//if it is even
{
sum+=1.0/(i+1);//since i is your k, you just divide by i+1
}
else
{
sum-=1.0/(i+1);
}
}
printf("pi = %g\n\n", sqrt(16*sum));
return 0;
}
```

But where I type a value, it print out

sqrt : DOMAIN ERROR

pie = + NAN

what does this mean?

I'm think is the "printf" that made my code went wrong.

0

Finally I got a value. This is what I got.

```
//*pi^2 / 16 = (-1)^k / k(k+1) (1 + (1/3) + (1/5) + ... + ( 1/((2*k)+1)))*\\
#include <stdio.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
int main()
{
int k, i;
double sum=0;
printf("Enter number of terms: ");
scanf("%d",&k);
for (i = 1; i <= k; i++)
{
if (i%2==0)
{
sum += (1.0) /(i+1);
}
else
{
sum -= (-1.0) /(i+1);
}
}
printf("pi = %g\n\n", sqrt(16*sum));
system("pause");
}
```

and come out with this

"Enter number of terms : 2

pi = 3.65148"

Have I done what I asked to do?

Thanks

You

This article has been dead for over six months: Start a new discussion instead