Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- bool isIncorrect;
- int precision;
- do {
- isIncorrect = false;
- cout << "Enter precision (number of iterations) : ";
- cin >> precision;
- if (cin.fail()) {
- cin.clear();
- cin.ignore();
- isIncorrect = true;
- }
- if (!isIncorrect && (precision <= 0)) {
- isIncorrect = true;
- }
- if (isIncorrect) {
- cout << "Input Error. Check if the data is correct." << endl;
- }
- while (cin.get() != '\n');
- } while (isIncorrect);
- double pi = 0.0;
- float numerator = 1;
- float firstDenominator = 1;
- float secondDenominator = 3;
- for (int i = 0; i < precision; i++) {
- for(int k = 0; k < 3; k++){
- if (k < 2){
- pi += numerator/(firstDenominator*secondDenominator);
- } else {
- pi -= numerator/(firstDenominator*secondDenominator);
- }
- firstDenominator+=4;
- secondDenominator+=4;
- }
- }
- cout << "Pi value = " << pi*8;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement