Monday, November 18, 2024
Google search engine
HomeData Modelling & AIPrint N to 1 without loop

Print N to 1 without loop

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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments