Last Updated: 14 Apr, 2022

Can You Print

Easy

Problem statement

You are given an integer ‘N’. You have to print integers from 1 to N.

For Example:
Input: 'N' = 6

Output: 1 2 3 4 5 6

If 'N' = 6, The numbers from 1 to 6 are 1, 2, 3, 4, 5, 6.
Hence, the output will be 1 2 3 4 5 6.
Input Format :
The first line of the input contains an integer, 'T’, denoting the number of test cases.

The first and only line of each test case will contain a single integer ‘N’.
Output Format :
For each test case, print the integers from 1 to N separated by spaces.

Print a separate line for each test case.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10^4

Time limit: 1 sec

Approaches

01 Approach

Run a for loop from 1 to N and print each number.

 

Here is the algorithm:

  1. Declare a vector ‘ans’ to store numbers.
  2. Iterate over the range from 1 to N (say iterator ‘i’)
    • Push ‘i’ into ‘ans’ vector.

02 Approach

The idea of this approach is to make a recursive function ‘print(i)’ which will print ‘i’ and it will stop when ‘i’ reaches ‘N + 1’.

 

Here is the algorithm:

  1. print function:
    • If ‘i’ is equal to ‘N+1’
      • Return
    • Push ‘i’ into ‘ans’ vector.
    • ‘print(i+1, n, ans)’.
  2. given function:
    • Declare a vector ‘ans’ to store numbers.
    • ‘print(1, N, ans)’.
    • Return ‘ans’.