Print N to 1 without loop Last Updated : 09 May, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice You are given an integer N. Print numbers from N to 1 without the help of loops. Examples: Input: N = 5Output: 5 4 3 2 1Explanation: We have to print numbers from 5 to 1. Input: N = 10Output: 10 9 8 7 6 5 4 3 2 1Explanation: We have to print numbers from 10 to 1. Approach: If we take a look at this problem carefully, we can see that the idea of "loop" is to track some counter value, e.g., "i = 0" till "i <= 100". So, if we aren't allowed to use loops, how can we track something? Well, one possibility is the use of 'recursion', provided we use the terminating condition carefully. Here is a solution that prints numbers using recursion. C++ // C++ program to How will you print // numbers from N to 1 without using a loop? #include <iostream> using namespace std; class gfg { // It prints numbers from N to 1 public: void printNos(unsigned int n) { if (n > 0) { cout << n << " "; printNos(n - 1); } return; } }; // Driver code int main() { int n = 10; gfg g; g.printNos(n); return 0; } C #include <stdio.h> // Prints numbers from N to 1 void printNos(unsigned int n) { if (n > 0) { printf("%d ", n); printNos(n - 1); } return; } // Driver code int main() { int n = 10; printNos(n); getchar(); return 0; } Java import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; class GFG { // Prints numbers from N to 1 static void printNos(int n) { if (n > 0) { System.out.print(n + " "); printNos(n - 1); } return; } // Driver Code public static void main(String[] args) { int n = 10; printNos(n); } } Python3 # Python3 program to Print # numbers from N to 1 def printNos(n): if n > 0: print(n, end=' ') printNos(n - 1) # Driver code n = 10 printNos(n) C# // C# code for print numbers from // N to 1 without using loop using System; class GFG { // Prints numbers from N to 1 static void printNos(int n) { if (n > 0) { Console.Write(n + " "); printNos(n - 1); } return; } // Driver Code public static void Main() { int n = 10; printNos(n); } } PHP <?php // PHP program print numbers // from N to 1 without // using loop // Prints numbers from N to 1 function printNos($n) { if($n > 0) { echo $n, " "; printNos($n - 1); } return; } // Driver code $n=10; printNos($n); ?> JavaScript // Javascript code for print numbers from // N to 1 without using loop // Prints numbers from N to 1 function printNos(n) { if(n > 0) { console.log(n + " "); printNos(n - 1); } return; } var n = 10; printNos(n); Output10 9 8 7 6 5 4 3 2 1 Time Complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Print N to 1 without loop R RishabhPrabhu Follow Improve Article Tags : DSA Recursion Practice Tags : Recursion Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like