Print 1 To N Without Loop | Practice | GeeksforGeeks

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 <= 105
0
yasugupta20011 week ago
class Solution{   void print(int N)   {       if(N == 0)       {           return;       }       else       {           print(N-1);           System.out.print(N + " ");       }   } public void printNos(int N)   {       //Your code here       print(N);   }} 
-1
kumarrohit5120001 week ago
// { Driver Code Starts//Initial Template for C
#include <stdio.h>
// } Driver Code Ends//User function Template for C
void printNos(int N){   if(N>0)   {       printNos(N-1);       printf("%d",N);          }//Your code here   return;}int main(){   printNos(10);   getchar();   return 0;}// { Driver Code Starts./* Driver program to test printNos */int main(){   int T;      //taking testcases   scanf("%d", &T);      while(T--)   {       int N;              //input N       scanf("%d", &N);       //calling printNos() function       printNos(N);       printf("\n");   }   return 0;} // } Driver Code Ends
0
kumarrohit5120001 week ago
 
#include<stdio.h>
void printNos(int N)
{
if(N>0)
{
printNos(N-1);
printf("%d",N);
}
return;
}
int main()
{
printNos(10);
getchar();
return 0;
}
 
0
niharbastia2971 week ago
 void printNos(int N)   {       //Your code here              if(N == 0)         return;                printNos(N-1);       cout<< N <<" ";   }
0
dipanshusharma93132 weeks ago
// java solution
class Solution{   public void print(int N){       if(N == 1){           return;       }       N = N-1;       print(N);       System.out.print(N+" ");   } public void printNos(int N)   {       //Your code here       print(N);       System.out.print(N+" ");   }}
0
dipanshusharma9313
This comment was deleted.
0
bhushanrane3 weeks ago
cout<<N<<" ";
0
shivjeetpaswan863 weeks ago
in cpp language
void print_n_number(int n)
{
if (n == 0)
{
return;
}
print_n_number(n - 1);
cout << n << " ";
}
 
0
sumit20445553 weeks ago
    if(N <= 0): return   self.printNos(N - 1)      print(N, end = " ")
0
sumit20445553 weeks ago
/Your code here       if(N==0)           return;       printNos(N-1);       cout<<N<<" ";   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.