Array ( [0] => [1] => questions [2] => Codevita-Previous-Questions [3] => Print-N-numbers ) Basic | Print N numbers | THE INQUISITIVE





Print N numbers

LEVEL:Beginner

Description

Given a Number N. You have to print all the numbers from 1 to N without using loops.

Input Format

First line contains a integer N.

Output Format

Print the numbers from 1 to N.


Example 1:

Input
5
Output
1 2 3 4 5
Example 2:

Input
8
Output
1 2 3 4 5 6 7 8
Example 3:

Input
10
Output
1 2 3 4 5 6 7 8 9 10

oops

Login to see Discussion




Approach

Approach 1: using static variable

Step-i) create a static variable and initialize with 1
Step-ii) create a empty string
Step-iii) append current value and call the present function by incrementing the variable
Step-iv) repeat step iii till i <= number
Step-v) return the string

Time Complexity: O(n)
Space Complexity: O(n)

Approach 2: without using static variable and recursion

Step-i) create a empty string and append the value i (initially i is equal to 1)
Step-ii) call the present function passing incremented i and number as parameters
Step-iii)repeat step ii till i <= num
Step-iv) return the string

Time Complexity: O(n)
Space Complexity: O(n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution