Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Recursive Call Example Sum
// clang 3.8.0 // Example from Lecture 5 in class on recursive functions #include <iostream> using namespace std; int recursiveAdd(int n) { int returnValue; for (int i=0; i<10-n; i++) cout << " "; cout << "Entering into recursiveAdd: n = " << n << endl; if (n==1) { returnValue = 1; } else { returnValue = n + recursiveAdd(n-1); } for (int i=0; i<10-n; i++) cout << " "; cout << "Returning from recursiveAdd: n = " << n << " -- Ans = " << returnValue << endl; return returnValue; } int main() { int n, answer; cout << "Enter n: "; cin >> n; cout << endl << "n = " << n << endl << endl; answer = recursiveAdd(n); cout << endl << "Triangle sum of " << n << " = " << answer << endl; return 0; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
EBO
auto Keyword Example
Range List for C++
Find in vector vs unordered_map
template specialization inheritance problem
Unqualified free functions
Virtual Function Example
Argument passing by using reference and value
user defined exception
constructor-is-not-called-in-this-aggregation-class
Please log in to post a comment.