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;
}
|