# Thread: Math equations not working

1. ## Math equations not working

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.

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. 2. There are several bugs there:
1. if(counter = 0) sets counter to 0 in stead of comparing it to 0. Use ==
2. If counter == 0, you do counter = counter - 1, making it -1. If it is 1, you do counter++, making it 2. So it doesn't switch properly between 0 and 1 as you'd like
3. 4/div. 4 is an integer, div is an integer, starting with 5. So the result is an integer, which is 4/5 truncated, which is 0. So you add/subtract 0, always. To make it a float, do 4.0/div instead. 3. Thank you very much, lot of silly mistakes i made on accident, except your 3rd point which i was not aware of, even after all your correction i found some simple things i misplaced. thanks again =] 4. also note that main should be
int main() Popular pages Recent additions 