fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include <cmath>
  5. #include <iomanip>
  6.  
  7. class Four
  8. {
  9. private:
  10. int inputedAccuracy;
  11. double pi;
  12. double denominator;
  13. int doneTermCounter;
  14. double oneTerm;
  15. int negativeController;
  16.  
  17. public:
  18. double question4()
  19. {
  20. cout << "Accuracy set at : " ;
  21. cin >> inputedAccuracy;
  22. cout << endl;
  23. pi = 0.0;
  24. denominator = 1.0;
  25. doneTermCounter = 0;
  26.  
  27. negativeController = 1;
  28. cout << "Term" << " " << "pi" << endl;
  29. cout << "1 " << " " << "4" << endl;
  30.  
  31.  
  32. for (inputedAccuracy; inputedAccuracy > 0; inputedAccuracy -= 100)
  33. {
  34. for (int doneTerm = 0; doneTerm < 100; doneTerm++)
  35. {
  36. pi = pi + (negativeController * 4 / denominator);
  37. negativeController *= -1;
  38. denominator += 2;
  39. doneTermCounter++;
  40. }
  41. if (doneTermCounter >= 10000)
  42. cout << doneTermCounter << " " << pi << endl;
  43. else
  44. if (doneTermCounter >= 1000)
  45. cout << doneTermCounter << " " << pi << endl;
  46. else
  47. cout << doneTermCounter << " " << pi << endl;
  48. }
  49. return 0.0;
  50. }
  51. };
  52.  
  53. int main()
  54. {
  55. Four f;
  56.  
  57. f.question4();
  58.  
  59. }
Success #stdin #stdout 0s 3300KB
stdin
1111
stdout
Accuracy set at : 
Term            pi
1               4
100             3.13159
200             3.13659
300             3.13826
400             3.13909
500             3.13959
600             3.13993
700             3.14016
800             3.14034
900             3.14048
1000            3.14059
1100            3.14068
1200            3.14076