Hi, I have given a task to write a code about approximate of pi. this is what I wrote from the other example.

the task is to :

(pi^2)/16 = ((-1)^k)/k+1 (1+1/3+1/5+...+1/(2k+1))

Write a program which computes and prints an approximation of pi using a finite number of terms from the above formula. The number of terms must be input from the keyboard (via scanf ). The program must also print an approximation error: the difference between the approximation and the value of pi stored in the macro constant M_PI, defined in <math.h>.

`//*pi^2 / 16 = (-1)^N / N(N+1) (1 + (1/3)) + (1/5) + ... + ( 1/((2*N)+1)))*\\

#include <stdio.h>
#include <math.h>
#ifndef M_PI 
#define M_PI 3.1415926535897932384626433832795
#endif

int main()
{  int N, i;
   double sum=0;

   printf("Enter number of terms: ");
   scanf("%d",&N);

   for (i = 1; i <= N; i++)
   {
     sum += 1.0/i/i; /* try 1.0/(i*i), N=100000 */
   }

   printf("pi = %g\n\n", sqrt(6*sum));}`

I'm not sure is this right or not, but i think this is wrong. so can someone give me some advice?

Thanks for the help

(-1)^k = 1 if k is even, -1 if k is odd so:

for (i=1; i<=N; 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);
    }
}

The rest is simply multiplying, then taking the square root.

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.

sqrt : DOMAIN ERROR
pie = + NAN
what does this mean?

It means sum is negative, most likely.

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

oops wrong code.
This is what I did.
I used sum = (sum + 1)/(i+1) and sum = (sum-1)/(i+1)
instead of += and -=

is come out with

enter.....terms : 2 or 20 (even)
pi = 1.63299 or 0.851886

but when I type 3(odd) is

sqrt : DOMAIN ERROR
pie = + NAN

Is the code right?

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.