Monday, 12 February 2018

how to sum of numbers using recursion

#include<iostream>
using namespace std;
int sum(int num)
         if(num==0)
         return 0;
         else return num+sum(num-1);
}
int main()
{
int n;
cout<<"Enter any number please"<<endl;
cin>>n;
cout<<"factorial is "<<sum(n)<<endl;
}

No comments:

Post a Comment