Advertisement
luizaspan

Aprox pi II

May 12th, 2015
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. // calcular valor aproximado de pi através da série de Leibniz
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. #define pi M_PI
  7.  
  8. int main(void)
  9. {
  10.   int i;
  11.   double s,x,e;
  12.  
  13.   s=0;
  14.  
  15.   for (i=0;i<=100;i++)
  16.     {
  17.       s+=(pow(-1,i)*1/(2*i+1)); // ENTENDEU POR QUE I+1???
  18.       printf("%lf\n",s);
  19.     }
  20.  
  21.   s=4*s;
  22.   x=pi;
  23.   e=fabs(((x-s)/x)*100);
  24.  
  25.   printf("\nValor calculado pela série: %lf \n",s);
  26.   printf("Valor esperado: %lf \n",x);
  27.   printf("Erro percentual: %.1lf% \n\n",e);
  28.    
  29.   return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement