Advertisement
wohyperion

Вычисление суммы ряда (Рекурсия)

Sep 25th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. float fSum(float n) {
  6.     if (n == 1)
  7.         return 1;
  8.     return (1 / n) + fSum(n - 1);
  9. }
  10.  
  11. int main() {
  12.     setlocale(LC_ALL, "russian");
  13.     float sum = 0;
  14.     float n;
  15.     cout << "Enter n: ";
  16.     cin >> n;
  17.     for (float i = 1; i <= n; i++) {
  18.         sum = sum + float(1 / i);
  19.     }
  20.     cout << "Sum = " << fSum(n) << endl;
  21.     cout << "Sum = " << sum << endl;
  22.     system("pause");
  23.     return 0;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement