Print Series Of Numbers Using Recursion in CPP

  1. Home
  2. Tutorials
  3. CPP
  4. CPP Programs
  5. Recursion
  6. Program

Source Code

#include <iostream>

using namespace std;

int printSeries ( int );

int main(int argc, char** argv) {
 

int a;  // variable declaration for input
    
    // Taking input in variables
    cout<<"Enter First Number : ";
    cin>>a;
    //function call
    cout<<printSeries(a);
   

return 0;
}

int printSeries ( int x) {
    
    cout<<x<<endl;
    x--;
    if(x == 1)   //base condition
        return 1;
    else
        return printSeries(x);  // recursive call
}

Output

print numbers series in c++ using recursion

Working

In this program example first of all a function is declared of name prnitSeries. Then in function defination ( after main function ) call to same method is palce in its own body.  This call is called recursive call. A base condition is also set in function body which determine the end of recursion.

Comments
Login to TRACK of Comments.