Estimating Root - C Data Structure

C examples for Data Structure:Algorithm

Description

Estimating Root

Demo Code

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

int main() {//w  w  w .  j  av a2  s  . co  m
   int maxCount = 10, amountLess;
   double r;

   srand(time(0));
   amountLess = 0;
   for (int h = 1; h <= maxCount; h++) {
      r = 2 + rand() / (RAND_MAX + 1.0);
      if (r * r < 5) 
         ++amountLess;
   }
   printf("\nAn approximation to the square root of 5 is %5.3f\n", 2 + (double) amountLess / maxCount);
}

Related Tutorials