Advertisement
vaibhav1906

Recursion

Dec 18th, 2021
1,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.35 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void helper(int n, vector<int> &v){
  5.    
  6.     if(n==0)return; //Base case
  7.    
  8.     //cout<<n<<" "; //Task
  9.     v.push_back(n);
  10.     helper(n-1,v); //Recursive step
  11.    
  12.    
  13. }
  14.  
  15. int main(){
  16.    
  17.     //to print 5,4,3,2,1
  18.    
  19.     int n = 5;
  20.     vector<int> v;
  21.     helper(n,v);
  22.    
  23.     for(int i = 0; i<v.size(); i++){
  24.         cout<<v[i]<<" ";
  25.     }
  26.    
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement