Approximating Pi with Loops help??

For my assignment, I need to do this:
The value of π can be determined by the series equation:

Write a program to approximate the value of π using the formula given.
>>>>>(SORRY IF YOU CAN'T READ THIS, IT'S A PICTURE THAT I CAN'T COPY)<<<<<

\pi=4\:\times\left(1-\:\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11}+\frac{1}{13}-...\right)

What to do:

Write a program that approximates the value of pi follows.

Ask the user for the the number of term desired. Therefore if the user asks for 5 terms then the approximation equals


Compute the approximate value of π by writing a loop that computes the sum of the terms then multiplying the sum by 4.
How do you alternate positive and negative terms in the loop?

Print the approximated value. Note that the more terms used, the better the approximation of π

I don't know how to alternate + and - like it has in the picture, or have it alternate denominators. Like how it has 1 then 3 then 5 as denominators...I can't do that...
My current code looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <stdio.h>

/* This program asks the user how many terms to use in order
 to estimate the value of Pi.*/
int main (){

int numTerms;
printf("How many terms would you like?\n");
	scanf("%d\n", &numTerms);

for (numTerms = 1; numTerms >= 1; numTerms++) {
double valPi = 4 * (1 / ((2 * numTerms - 1)));
printf("%lf", &valPi);	
}

return 0;
}
You need a -1 in there somewhere to make the algorithm generate + and -.
@kbw
Where would that be though? If I need to alternate using + and -, where would I put the -1 in order to do that? I feel like you'd want to put it somewhere in the valPi equation, but I have no clue where...
You're trying to implement Leibnitz's method for calculating Pi although Madhava got there a few centuries earlier.
https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
https://en.wikipedia.org/wiki/Madhava_of_Sangamagrama

Well, you can see where the -1 comes in.

You can implement this with a for loop and check for the index being even for + and odd for -.
So would it look something like
(-1^i)/(2*i-1)?

If so, would that actually be it to the code..?
Thank you for the help by the way!
More like:
1
2
3
4
5
6
7
double pi{1};
for (size_t i = 1; i != mx; ++i)
    if (i%2 == 0)
        pi += 1.0 / (2*i + 1);
    else
        pi += -1.0 / (2*i + 1);
pi *= 4;
Last edited on
So this is what I have, but it just runs, I type in my number, and nothing happens..
If I type in 8, nothing happens until I hit another key. Then it prints "0.000000" 7 times. Whatever number I type in, it prints that 1 less than my number. I have no idea how to use loops or anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

/* This program asks the user how many terms to use in order
 to estimate the value of Pi.*/
int main (){

int numTerms;
printf("How many terms would you like?\n");
	scanf("%d\n", &numTerms);
	
for (int i = 1; i <= numTerms - 1; i++) {
	double valPi;
	if (i % 2 == 0) {
		valPi = 4 * (1 / 2 * i + 1);
	}
	else {
		valPi = 4 * (-1 / 2 * i +1);
	}

printf(" %lf", &valPi);
}
return 0;
}
Last edited on
Your algorithm is very different from mine.

You're using printf incorrectly. Are you using C or C++? If you're using C++, get used to doing I/O using the iostream library.
Last edited on
C++
I don't know what Iostream is. My class is only only loops, so we don't know a whole lot. That's what's making this assignment so hard; We know almost nothing. T_T
Last edited on
Ok, you have to start somewhere, so that's ok. Check my code and correct your code; it's important that you see the difference

The correct way to print valPi is:
 
printf(" %f\n", valPi);
Last edited on
Topic archived. No new replies allowed.