Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <iomanip>
- using namespace std;
- int precision = 15;
- int spacing = precision + 4;
- bool processAnswer(string answer)
- {
- return answer == "Y" || answer == "y";
- }
- long double doCalculation(int n)
- {
- if(n == 1)
- {
- return 1.0 / 3;
- }
- else
- {
- long double result = 1.0 / (2 * n + 1);
- while(n > 1)
- {
- result = 1 / (result + 2 * n - 1);
- n--;
- }
- return result;
- }
- }
- main()
- {
- system(" chcp 1251 > nul");
- bool isFinished; //условие многократной работы программы
- string answer; //объявление переменных
- int n;
- do
- {
- cout << endl << "Натуральное n = ";
- cin >> n;
- cout << "Вычисленное значение y = ";
- cout << fixed << setw(spacing) << setprecision(precision) << doCalculation(n);
- cout << endl << "Продолжить работу? (y,n)"; //многократная работа
- cin >> answer;
- isFinished = processAnswer(answer);
- }
- while(isFinished);
- }
Add Comment
Please, Sign In to add comment