Advertisement
Guest User

Compute pi

a guest
Oct 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.     double compute_pi(int N){
  4.         double i=2,j=1;
  5.  
  6.         int cont=0;
  7.  
  8.         double mult = 1;
  9.  
  10.         while(cont<N){
  11.  
  12.             mult = mult * (i/j);
  13.  
  14.             if(cont%2==0) j = j + 2;
  15.                 else i = i + 2;
  16.            
  17.  
  18.  
  19.             cont++;
  20.         }
  21.  
  22.     return mult*2;
  23.  
  24.     }
  25.  
  26. int main(){
  27.     int N;
  28.  
  29.     double pi;
  30.  
  31.     scanf("%d", &N);
  32.  
  33.     pi = compute_pi(N);
  34.  
  35.     printf("%.12lf", pi);
  36.  
  37.     return 0;
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement