Estimating Pi - C Data Structure

C examples for Data Structure:Algorithm

Description

Estimating Pi

Demo Code

#include <stdio.h>
#include <stdlib.h>

int main() {/*from  w  ww .j ava2 s.c  om*/
  int inS = 10, inC = 0;
  double x, y;

  //srand(time(0));
  for (int h = 1; h <= inS; h++) {
    x = rand() / (RAND_MAX + 1.0);
    y = rand() / (RAND_MAX + 1.0);
    if (x * x + y * y <= 1)
      inC++;
  }
  printf("\nAn approximation to pi is %5.3f\n", 4.0 * inC / inS);
}

Related Tutorials