Showing posts with label First n Natural Numbers. Show all posts
Showing posts with label First n Natural Numbers. Show all posts

Saturday, August 22, 2015

Finding Sum using Recursive Function

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;
}