fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. double pi = 0.0;
  7. long i;
  8. int n;
  9.  
  10. cout << "Enter the value of n: "; //prompt for input
  11. cin >> n; //store input in "n"
  12. cout << endl; //end line
  13.  
  14. for (i = 0; i < n; i++)
  15. {
  16. if (i % 2 == 0) //if even
  17. pi = pi + (1.0 / (2.0 * i + 1.0));
  18. else //if odd
  19. pi = pi - (1.0 / (2.0 * i + 1.0));
  20. }
  21.  
  22. pi = 4.0 * pi; //multiply by 4
  23.  
  24. cout << endl << "pi = " << pi << endl; //display result
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 3460KB
stdin
5
stdout
Enter the value of n: 

pi = 3.33968