im trying to sum integers, but i dont knw where the flaw is in this code
just tell me where is the flaw in this code, dont give other ways to solve this question
Because of the nature of recursion in that it calls itself infinitely unless an exit statement is reached, any type of recursion needs an exit statement (unless you're researching stuff and specifically want to crash something)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int SUM(int n) {
cout<<n<<endl;
if(n==0)
return n;
elsereturn n += SUM(n-1);
return n ;
}
int main() {
cout<<SUM(3)<<endl;
}
I gave your code an exit statement and fixed up some syntax