Advertisement
2607

Untitled

Mar 12th, 2020
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main() {
  5.     float pi = 0, absadd, precision, add, middle, end_pi;
  6.     int n, i = -1;
  7.     printf("Enter number of digits (1-7): ");
  8.     scanf("%d", &n);
  9.  
  10.     precision = pow(0.1, n);
  11.  
  12.     do
  13.     {
  14.         i++;
  15.         add = 4*pow(-1, i)/(2*i+1);
  16.         pi += add;
  17.         absadd = fabs(add);
  18.     } while (absadd>=precision);
  19.  
  20.     middle = -(add/2);
  21.     printf("step    %d\n",i);
  22.     pi += middle;
  23.     end_pi = (float)(((int)(pi*pow(10,n)))/pow(10,n));
  24.     printf("   pi = %.*f\n\n",n,end_pi);
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement