Can comeone expain me with the function of this program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

7th May 2018, 2:26 AM
Vikram Vm
Vikram Vm - avatar
1 Answer
+ 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.
30th May 2018, 8:23 AM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar