Monday, October 7, 2024
Google search engine
HomeData Modelling & AIPrint 1 to n without using loops

Print 1 to n without using loops

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

Examples:

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

Input: N = 10
Output: 1 2 3 4 5 6 7 8 9 10
Explanation: We have to print numbers from 1 to 10.

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 1 to n without using a loop?
#include <iostream>
using namespace std;

class gfg {

    // It prints numbers from 1 to n.
public:
    void printNos(unsigned int n)
    {
        if (n > 0) {
            printNos(n - 1);
            cout << n << " ";
        }
        return;
    }
};

// Driver code
int main()
{
    int n = 10;
    gfg g;
    g.printNos(n);
    return 0;
}

C

#include <stdio.h>

// Prints numbers from 1 to n
void printNos(unsigned int n)
{
    if (n > 0) {
        printNos(n - 1);
        printf("%d ", n);
    }
    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 1 to n
    static void printNos(int n)
    {
        if (n > 0) {
            printNos(n - 1);
            System.out.print(n + " ");
        }
        return;
    }

    // Driver Code
    public static void main(String[] args)
    {
        int n = 10;
        printNos(n);
    }
}

Python3

# Python3 program to Print
# numbers from 1 to n


def printNos(n):
    if n > 0:
        printNos(n - 1)
        print(n, end=' ')


# Driver code
n = 10
printNos(n)

C#

// C# code for print numbers from
// 1 to n without using loop
using System;

class GFG {

    // Prints numbers from 1 to n
    static void printNos(int n)
    {
        if (n > 0) {
            printNos(n - 1);
            Console.Write(n + " ");
        }
        return;
    }

    // Driver Code
    public static void Main()
    {
        int n = 10;
        printNos(n);
    }
}

PHP

<?php
// PHP program print numbers 
// from 1 to n without 
// using loop    

// Prints numbers from 1 to n
function printNos($n)
{
    if($n > 0)
    {
        printNos($n - 1);
        echo $n, " ";
    }
    return;
}

// Driver code
$n=10;
printNos($n);
?>

Javascript

// Javascript code for print numbers from 
    // 1 to n without using loop
    
    // Prints numbers from 1 to n
    function printNos(n)
    {
        if(n > 0)
        {
            printNos(n - 1);
            console.log(n + " ");
        }
        return;
    }
    
    var n = 10;
    printNos(n);
Output

1 2 3 4 5 6 7 8 9 10 

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