This commit is contained in:
NaiJi ✨ 2021-08-31 12:42:13 +03:00
commit ad1ff383ef
3 changed files with 34 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.o

14
4.1/sum.cpp Normal file
View File

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

19
4.1/sum_tail.cpp Normal file
View File

@ -0,0 +1,19 @@
#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';
}