fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sumToN(int n) {
  5. if (n == 1)
  6. return 1;
  7. return n + sumToN(n - 1);
  8. }
  9.  
  10. int main() {
  11. int n;
  12. cout << "Введіть n: ";
  13. cin >> n;
  14.  
  15. if (n <= 0) {
  16. cout << "Помилка! n повинно бути додатним цілим числом." << endl;
  17. return 0;
  18. }
  19.  
  20. cout << "Сума 1.." << n << " = " << sumToN(n) << endl;
  21.  
  22. return 0;
  23. }
Success #stdin #stdout 0.01s 5288KB
stdin
7
stdout
Введіть n: Сума 1..7 = 28