« Write a program to print numbers from 1 to n in increasing order recursively

Problem:

Write a program to print numbers from 1 to n in increasing order recursively.

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

1#include<iostream>
2using namespace std;
3#include "Solution.h"
4
5void print(int n){
6 if(n == 1){
7 cout << n << " ";
8 return;
9 }
10 print(n - 1);
11 cout << n << " ";
12}
13
14int main(){
15 int n;
16 cin >> n;
17
18 print(n);
19}