What i need to do is write a program that shows the value of PI based on the following formula:
PI = 4 - 4/3 +4/5 - 4/7 + 4/9 - 4/11 + ...
Then show the result as a table that shows the PI value and the number of terms in the formula that were used. For example:
1 4
2 2.6667
3 3.46667
4 2.895
...
Note that:
2.6667 = 4 - 4/3
3.46667 = 4 – 4/3 + 4/6
The program should first ask the number of rows in the table.
This is what i made:
Code:
#include "stdio.h"
int rows; //rows user inputs
int div = 3; // divisor
int index = 1; //printed index
int counter = 1; // keeps track of even/odd index
float pi = 4; //pi value
void main() {
printf("Please Enter the number of rows to be generated: \n");
scanf("%d", &rows);
if (rows >= 1) {
printf("%d %f\n",index, pi);
div = div+2;
printf("div");
}
while(index <= rows) {
if(counter = 0){ //even
pi = pi - 4/div;
index++;
printf("%d %f\n",index, pi);
div = div + 2;
counter = counter - 1;
}
else { //odd (counter = 1)
pi = pi + 4/div;
index++;
printf("%d %f\n",index, pi);
div = div + 2;
counter++;
}
}
}
What i get is:
1 4.000000
2 4.000000
3 4.000000
4 4.000000
5 4.000000
and so on. the problem is probably within the "div = div + 2" i dont know how to write that and im pretty sure what i did makes no sense, and ive probably butchered all other mathematical statements aswell.