The Wayback Machine - https://web.archive.org/web/20240717180320/https://www.geeksforgeeks.org/print-n-to-1-without-loop/
Open In App

Print N to 1 without loop

Last Updated : 09 May, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

You are given an integer N. Print numbers from N to 1 without the help of loops.

Examples:

Input: N = 5
Output: 5 4 3 2 1
Explanation: We have to print numbers from 5 to 1.

Input: N = 10
Output: 10 9 8 7 6 5 4 3 2 1
Explanation: 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);
Output

10 9 8 7 6 5 4 3 2 1 

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


Previous Article
Next Article

Similar Reads

Print 1 to 100 without loop using Goto and Recursive-main
Our task is to print all numbers from 1 to 100 without using a loop. There are many ways to print numbers from 1 to 100 without using a loop. Two of them are the goto statement and the recursive main. Print numbers from 1 to 100 Using Goto statement Follow the steps mentioned below to implement the goto statement: declare variable i of value 0decla
5 min read
Print a pattern without using any loop
Given a number n, print the following pattern without using any loop. n, n-5, n-10, ..., 0, 5, 10, ..., n-5, n Examples : Input: n = 16Output: 16, 11, 6, 1, -4, 1, 6, 11, 16 Input: n = 10Output: 10, 5, 0, 5, 10 We strongly recommend that you click here and practice it, before moving on to the solution.Print a pattern without using any loop (using r
11 min read
How will you print numbers from 1 to 100 without using a loop?
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 &lt;= 100". So, if we aren't allowed to use loops, how can we track something in the C language?Well, one possibility is the use of 'recursion', provided we use the terminating condition carefully. Here is a solution
12 min read
Time taken by Loop unrolling vs Normal loop
We have discussed loop unrolling. The idea is to increase performance by grouping loop statements so that there are less number of loop control instruction and loop test instructions C/C++ Code // CPP program to compare normal loops and // loops with unrolling technique #include &lt;iostream&gt; #include &lt;time.h&gt; using namespace std; int main
6 min read
Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
For such cases, time complexity of the loop is O(log(log(n))).The following cases analyse different aspects of the problem. Case 1 : for (int i = 2; i &lt;=n; i = pow(i, k)) { // some O(1) expressions or statements } In this case, i takes values 2, 2k, (2k)k = 2k2, (2k2)k = 2k3, ..., 2klogk(log(n)). The last term must be less than or equal to n, an
1 min read
Printing all subsets of {1,2,3,...n} without using array or loop
Given a natural number n, print all the subsets of the set [Tex]\{1, 2, 3, ..., n\} [/Tex]without using any array or loop (only the use of recursion is allowed).Examples: Input : n = 4 Output : { 1 2 3 4 } { 1 2 3 } { 1 2 4 } { 1 2 } { 1 3 4 } { 1 3 } { 1 4 } { 1 } { 2 3 4 } { 2 3 } { 2 4 } { 2 } { 3 4 } { 3 } { 4 } { } Input : n = 2 Output : { 1 2
8 min read
Print a 2D Array or Matrix using single loop
Given a matrix mat[][] of N * M dimensions, the task is to print the elements of the matrix using a single for loop. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output: 1 2 3 4 5 6 7 8 9 Input: mat[][] = {{7, 9}, {10, 34}, {12, 15}}Output: 7 9 10 34 12 15 Approach: To traverse the given matrix using a single loop, observe that there
5 min read
Print pattern using only one loop | Set 1 (Using setw)
Print simple patterns like below using single line of code under loop. Examples: Input : 5Output : * ** *** *********Input : 6Output : * ** *** **** ***********setw(n) Creates n columns and fills these n columns from right. We fill i of them with a given character, here we create a string with i asterisks using string constructor. setfill() Used to
4 min read
Print the pattern by using one loop | Set 2 (Using Continue Statement)
Given a number n, print triangular pattern. We are allowed to use only one loop.Example: Input: 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * We use single for-loop and in the loop we maintain two variables for line count and current star count. If current star count is less than current line count, we print a star and continue.
5 min read
Inorder Tree Traversal without recursion and without stack!
Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree. 1. Initialize current as root 2. While current
9 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg