Advertisement
Atheuz

Untitled

Oct 24th, 2011
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.49 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double approximate_pi();
  4.  
  5. int main(void) {
  6.     printf("pi is approximately equal to %f", approximate_pi());
  7.  
  8.     return 0;
  9. }
  10.  
  11. double approximate_pi() {
  12.     double pi = 1.0;
  13.     int parity = 0;
  14.     int i;
  15.  
  16.     for(i=3; i <= 99; i+=2) {
  17.         if(parity == 0) {
  18.             pi -= (1.0/i);
  19.             parity = 1;
  20.         }
  21.         else if(parity == 1) {
  22.             pi += (1.0/i);
  23.             parity = 0;
  24.         }
  25.     }
  26.  
  27.     pi *= 4.0;
  28.  
  29.     return pi;
  30. }
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement