Sunday 29 June 2014

C++ program to print first n natural numbers using recursion


Here is a C++ program to print first n natural numbers using recursion


 SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
void print(int n)
{
    if(n==0)
        return ;
    print(n-1);
    cout<<n<<" ";
}
int main()
{
    int n;
    cout<<"Enter the value of n ";
    cin>>n;
    print(n);
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment