Q. 105. Program to find the Sum of first 'n' natural numbers using Recursive function.
Copy Code:#include <iostream>
using namespace std;
int sum(int n)
   {
      int f;
      if (n==1)
         return (1);
      else 
         f = n + sum (n-1);
         return (f);
   }
int main()
{
 int n;
 cout << "Enter any Positive Number : ";
 cin >> n;
 cout << "Sum of first " << n << " numbers is : " << sum(n);
 return 0;
}


 
No comments:
Post a Comment