# [Question] Approximate of pi

• (2 Pages)
• 1
• 2

## 19 Replies - 1222 Views - Last Post: 01 February 2013 - 09:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310330&amp;s=d9cabe13de4cf218e787595f716f12af&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 qqaa007

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 30-January 13

# [Question] Approximate of pi

Posted 30 January 2013 - 10:34 AM

Hi , I'm new to c programming. I have been giving a task to do and I got trouble in my code.
Can someone have a look my code and give me some advice?

The task is this:

(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  stored in the macro constant M_PI, defined in <math.h>.
Please start you program from

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

and this is what I wrote:

```//*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");
}
```

I'm getting this:

```Enter number of terms: 2

sqrt: DOMAIN error
pi = +NAN
```

When I put any value, I'm still getting this.
I'm not sure where did the code went wrong.
So can I get some help?
thanks

Is This A Good Question/Topic? 0

## Replies To: [Question] Approximate of pi

### #2 mojo666

Reputation: 397
• Posts: 856
• Joined: 27-June 09

## Re: [Question] Approximate of pi

Posted 30 January 2013 - 11:04 AM

I'm not sure that you have the right formula. As written, the right side of the formula is sometimes negative which can't be equal to the left side.

As for your code, it is always negative which is why I think it is failing when you try to take the squareroot. i should start at 0, not 1.

### #3 qqaa007

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 30-January 13

## Re: [Question] Approximate of pi

Posted 30 January 2013 - 12:26 PM

I just rewrite the code

```#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 = 0 ; i <= k; i++)
{
sum = pow(-1.0,k) /(k+1);  }

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

I got a value now but only for the even number and not for the odd number.
the odd number is still DOMAIN error

thanks for the help.

### #4 qqaa007

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 30-January 13

## Re: [Question] Approximate of pi

Posted 30 January 2013 - 12:48 PM

I just change the k to i,

```    sum = pow(-1.0,i) /(i+1);
```

and I got a value now.
Is the code right?

### #5 mojo666

Reputation: 397
• Posts: 856
• Joined: 27-June 09

## Re: [Question] Approximate of pi

Posted 30 January 2013 - 03:45 PM

Did you get anything close to pi?

I am assuming not so you don't have the right code. Your code does not resemble the formula you posted and that formula doesn't even appear to be correct.

### #6 qqaa007

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 30-January 13

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 04:51 AM

Yeah I got something like 3.xxxxx whatever number I put it in

enter...terms : 5
pi = 3.14113

### #7 qqaa007

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 30-January 13

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 06:21 AM

the formula is this:

file:///C:/Users/Egg/Desktop/f.png

### #8 Skydiver

• Code herder

Reputation: 4768
• Posts: 15,750
• Joined: 05-May 12

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 06:22 AM

Sorry, we can't read files that are sitting on your machine. You'll have to upload the picture.

### #9 qqaa007

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 30-January 13

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 06:26 AM

the formula is this:
```
(pi)^2    (-1)^k
______  = ______  ( 1 +

16       k+1

```

they giving me the formula is this:

```(pi)^2    (-1)^k        1   1          1
______  = ______  ( 1 + _ + _ + ... + ____ )

16       k+1          3   5         2k+1

```

This post has been edited by Skydiver: 31 January 2013 - 08:09 AM
Reason for edit:: Put formulas into code tags to preserve spaces.

### #10 qqaa007

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 30-January 13

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 06:35 AM

Sorry about the reply ...I can't attach the file for the formula.

Anyway the formula they given me is

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

I'm not sure where my code went wrong..

thanks

### #11 mojo666

Reputation: 397
• Posts: 856
• Joined: 27-June 09

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 09:31 AM

There are a couple issues here. First, the formula you posted doesn't make any sense. For example, if we plug in 5 then we get....

(-1^5)/6 (1+1/3+1/5+1/7+1/9) = -0.29788...

Since it is a negative number, we cannot take the square root without resorting to imaginary numbers.

When I plug 5 into your code, I also get something very close to pi...but when I plug other numbers in the result is off. For example, if we plug in 11 the code produces 3.232858.... The code just happens to work well for 5.

### #12 #define

• Duke of Err

Reputation: 1732
• Posts: 6,158
• Joined: 19-February 09

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 11:36 AM

Looks related to Leibniz formula for π.

I think it must be a sequence.

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

Assuming the formula is correct.
I don't know whether this part should be calculated once or for each k.

```(1 + 1/3 + 1/5 +...+ 1/(2k+1))
```

### #13 mojo666

Reputation: 397
• Posts: 856
• Joined: 27-June 09

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 02:03 PM

Quote

I don't know whether this part should be calculated once or for each k.

It does work pretty well if you calculate it for each k.

qqaa007, sum += pow(-1.0,i) /(i+1); only accounts for the first part of the formula. The actual code needs to be
sum += seq * pow(-1.0,i) / (i+1); where seq is equal to (1+1/3+1/5...+1/(2i+1))

This post has been edited by mojo666: 31 January 2013 - 03:52 PM

### #14 qqaa007

• New D.I.C Head

Reputation: 0
• Posts: 16
• Joined: 30-January 13

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 04:49 PM

OK. thanks.
I added a few lines there and this is what I got for now.
I think is still wrong I got 3.14... only for 4 and not for other

```int main()
{
int k, i;
double sum,pi , seq  = 0;

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

for (i = 1; i <= k; i++)
{
seq += 1.0 / ((2 * i) + 1 ) ;
}

for (i = 0 ; i <= k; i++)
{
sum += pow(-1.0,i) / (i + 1.0);
}

pi = sqrt(16 * (sum * seq));

printf("pi = %f\n\n", pi);
```

### #15 #define

• Duke of Err

Reputation: 1732
• Posts: 6,158
• Joined: 19-February 09

## Re: [Question] Approximate of pi

Posted 31 January 2013 - 07:20 PM

In the sequence what is the calculate value for the first term, and what should the value be?

```09	    for (i = 1; i <= k; i++)

```

How many times does this loop when k is 2?

```14	    for (i = 0 ; i <= k; i++)

```