[ create a new paste ] login | about

Link: http://codepad.org/jIyju5ym    [ raw code | output | fork ]

mohit_at_codepad - C, pasted on Apr 26:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* Print the value of pi */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define DECIMAL_ACCURACY 1E-6

/**
 * Using Vietis equation
 */

int main() {
  double fac = sqrt(2.0);
  double val = 1.0;
  do {
    val *= fac / 2.0;
    fac = sqrt(2 + fac);
  } while(2.0 - fac > DECIMAL_ACCURACY);
  printf("Value of pi is: %g\n", 2.0 / val);
  return 0;
}


Output:
1
Value of pi is: 3.14159


Create a new paste based on this one


Comments: