fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. double x;
  9. int n;
  10.  
  11. cout << "Enter x: ";
  12. cin >> x;
  13. cout << "Enter number of terms (n): ";
  14. cin >> n;
  15.  
  16. double sum = 0.0;
  17. double term = (x * x) / 2.0; // Перший член ряду при n=1: x^2 / 2!!
  18.  
  19. for (int i = 1; i <= n; ++i) {
  20. sum += term;
  21.  
  22. // Рекурентний перехід до наступного члена:
  23. // a(i+1) = a(i) * (x^2 / (2 * (i + 1)))
  24. term *= (x * x) / (2.0 * (i + 1));
  25. }
  26.  
  27. cout << fixed << setprecision(10);
  28. cout << "Sum (O(n)): " << sum << endl;
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 5248KB
stdin
1
3
stdout
Enter x: Enter number of terms (n): Sum (O(n)): 0.6458333333