Print numbers from 1 to N without the help of loops.
Example 1:
Input:
N = 10
Output: 1 2 3 4 5 6 7 8 9 10
Example 2:
Input:
N = 5
Output: 1 2 3 4 5
 
Your Task:
This is a function problem. You only need to complete the function printNos() that takes N as parameter and prints number from 1 to N recursively. Don't print newline, it will be added by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N) (Recursive).
Constraints:
1 <= N <= 1000

Source: geeksforgeeks | Difficulty: EASY
Tags: Complete search

Starter code: #User function Template for python3



class Solution:    

    #Complete this function

    def printNos(self,N):

        #Your code here

Solutions:
class Solution:

	def printNos(self, N):
		if N > 0:
			self.printNos(N - 1)
			print(N, end=' ')


class Solution:

	def printNos(self, N):
		for i in range(1, N + 1):
			print(i, end=' ')


class Solution:
	num = 1

	def printNos(self, N):
		if self.num > N:
			return
		print(self.num, end=' ')
		self.num += 1
		self.printNos(N)


class Solution:

	def printNos(self, N):

		def x(i, j):
			if i > j:
				return
			print(i, end=' ')
			x(i + 1, j)
		x(1, N)


class Solution:

	def printNos(self, N):

		def helper(N):
			if N == 0:
				return
			helper(N - 1)
			print(N, end=' ')
		helper(N)


class Solution:

	def printNos(self, N):
		if N == 0:
			return
		else:
			ob = Solution()
			ob.printNos(N - 1)
			print(N, end=' ')


class Solution:

	def printNos(self, N):
		if N == 1:
			print(1, end=' ')
		else:
			temp = N
			self.printNos(N - 1)
			print(temp, end=' ')


class Solution:

	def printNos(self, n):
		c = n - 1
		self.helper(c, n)

	def helper(self, c, n):
		if c == 0:
			print(n, end=' ')
			return
		print(n - c, end=' ')
		self.helper(c - 1, n)


class Solution:

	def printNos(self, N):

		def rec(i, n):
			print(i, end=' ')
			i = i + 1
			if i == n + 1:
				return
			else:
				return rec(i, n)
		rec(1, N)


class Solution:

	def printNos(self, N):

		def f(n):
			if n == 1:
				print(1, end=' ')
				return
			f(n - 1)
			print(n, end=' ')
		f(N)


class Solution:

	def printNos(self, N):

		def recursive(i):
			if i == N + 1:
				return
			print(i, end=' ')
			recursive(i + 1)
		recursive(1)


class Solution:

	def printNos(self, N):

		def help(n):
			if n == N + 1:
				return
			else:
				print(n, end=' ')
			help(n + 1)
		help(1)


class Solution:

	def printNos(self, N):

		def p(i, N):
			if i < N + 1:
				print(i, end=' ')
				p(i + 1, N)
		p(1, N)


class Solution:

	def printNos(self, n):

		def order(i):
			if i == n + 1:
				return
			print(i, end=' ')
			order(i + 1)
		order(1)


a = 1

class Solution:

	def printNos(self, N):
		global a
		if a > N:
			a = 1
			return
		print(a, end=' ')
		a += 1
		self.printNos(N)


class Solution:

	def printNos(self, N):

		def sol(x):
			if x > N:
				return
			else:
				print(x, end=' ')
				sol(x + 1)
		return sol(1)