Advertisement
Sauka1337

Untitled

Sep 25th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6.     bool isIncorrect;
  7.     int precision;
  8.  
  9.     do {
  10.         isIncorrect = false;
  11.  
  12.         cout << "Enter precision (number of iterations) : ";
  13.         cin >> precision;
  14.  
  15.         if (cin.fail()) {
  16.             cin.clear();
  17.             cin.ignore();
  18.             isIncorrect = true;
  19.         }
  20.  
  21.         if (!isIncorrect && (precision <= 0)) {
  22.             isIncorrect = true;
  23.         }
  24.  
  25.         if (isIncorrect) {
  26.             cout << "Input Error. Check if the data is correct." << endl;
  27.         }
  28.  
  29.         while (cin.get() != '\n');
  30.     } while (isIncorrect);
  31.  
  32.     double pi = 0.0;
  33.     float numerator = 1;
  34.     float firstDenominator = 1;
  35.     float secondDenominator = 3;
  36.  
  37.     for (int i = 0; i < precision; i++) {
  38.         for(int k = 0; k < 3; k++){
  39.             if (k < 2){
  40.                 pi += numerator/(firstDenominator*secondDenominator);    
  41.             } else {
  42.                 pi -= numerator/(firstDenominator*secondDenominator);
  43.             }
  44.             firstDenominator+=4;
  45.             secondDenominator+=4;
  46.         }
  47.     }
  48.  
  49.     cout << "Pi value = " << pi*8;
  50.  
  51.     return 0;
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement