grokk-exs/4.1/sum_tail.cpp

20 lines
263 B
C++

#include <iostream>
int sumStep(int n, int s)
{
return (n != 0)
? sumStep(n - 1, n + s)
: s;
}
int sum(int n)
{
return sumStep(n, 0);
}
int main()
{
int n = 5;
std::cout << "For n = " << n << " sum is " << sum(n) << '\n';
}