int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
int main() {
int result = sum(10);
cout << result;
return 0;
}
It's a C++ code I don't understand, when you return in the 3rd like (return k + sum(k-1); aren't we returning 10 + 9 together? Not like 10+9+8+7+6+5+4+3+2+1=55 (Original Output)
shouldn't this be the output? by this I mean like the return is something like 10+9 then again 9+8 again 8+7 So the output might be like 10+9+9+8+8+7+7+6+6+5+5+4+4+3+3+2+2+1+1+0+0? (ac to me the sum would be like this) The output would be one hundred?
I know I am wrong but please clear this for me.