0
Can comeone expain me with the function of this program
#include<iostream> using namespace std; int add(int n); int main() { int n; cout << "Enter a positive integer: "; cin >> n; cout << "Sum = " << add(n); return 0; } int add(int n) { if(n != 0) return n + add(n - 1); return 0; }
1 Réponse
+ 1
This add function recursively adding number from 1 to the number the number provided by user.
If input number is 5 then program will add numbers in below fashion using recursion,
5+4+3+2+1
So output will be 15.