Print numbers from 1 to 100 without using a loop using C/C++/Python/Java

In C

We use a function to display the numbers from 1 to 100 declaring an int and applying conditions then calling the function.

#include<stdio.h>
int n(int i)
{
    if(i<=100) {
        printf("%d\t",i);
        n(i+1);
    }
}
int main()
{
    int i = 1;
    n(i);
    return 0;
}

The output :

1          2          3          4          5          6            7        8         9         10        11        12        13        14        15            16        17        18        19        20        21        22        23        24        25        26        27        28        29            30        31        32        33        34        35        36        37        38        39        40        41        42        43            44        45        46        47        48        49        50        51        52        53        54        55        56        57            58        59        60        61        62        63        64        65        66        67        68        69        70        71            72        73        74        75       76        77        78        79        80        81        82        83        84        85            86        87        88        89        90        91        92        93        94        95        96        97        98        99            100     

In C++:

Only by changing the input and output statements, we can get the code for C++

#include <iostream>
using namespace std;
int n=100;
 
int main()
{
    static int i = 0;
    if (i++ < n)
    {
        cout << i << " ";
        main();
    }
 
    return 0;
}

The output :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

In Python:

Python is a much easier language used to do programs. Here, a function is defined and then called as per the needs

def main(i):
     
    if (i <= 100):
        print(i, end = " ")
        i = i + 1
        main(i)
         
i = 1
main(i)

The output :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

In Java:

We have a function and limits as upper and lower.  Increasing the lower value till it reaches 101 then it will stop working.

import java.io.*;

class GFG {
	public static void main(String[] args)
	{
		printN(1, 100);
	}
	public static void printN(int lower, int upper)
	{
		if (lower<= upper) {
			System.out.print(lower + " ");
			printN(lower + 1, upper);
		}
	}
}

The output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Time complexity -:0(n) – it is the time taken by an algorithm to run as a function of the length of the input.

Space Complexity : 0(n)- the number of total spaces taken by an algorithm to run as a function of the length of the input.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *