Given is the code to print numbers from 1 to n in increasing order recursively. But it contains few bugs that you need to rectify such that all the test cases pass.

PROBLEM:-Given is the code to print numbers from 1 to n in increasing order recursively. But it contains few bugs that you need to rectify such that all the test cases pass.

Input Format :
Integer n
Output Format :
Numbers from 1 to n (separated by space)
Constraints :
1 <= n <= 10000
Sample Input 1 :
 6
Sample Output 1 :
1 2 3 4 5 6
Sample Input 2 :
 4
Sample Output 2 :
1 2 3 4

SOLUTION:-

#include<iostream>
using namespace std;
void print(int n){
    //if(n>=1&&n<=10000)
    //{
    static int i=1;
    if(n == 1){
        cout << i << " ";
        return;
    }

    cout << i << " ";
        i++;
    print(n - 1);
    //}
}

int main(){
    int n;
    cin >> n;
  
    print(n);
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *