How to Print Numbers From 1 to 100 Without Using a Loop {while, do while, for}?


 

In the realm of programming, there's often more than one way to accomplish a task. When it comes to printing numbers from 1 to 100 without using traditional loops like `for`, `while`, or `do while`, creativity and ingenuity come into play. In this article, we'll delve into three distinct methods to achieve this goal using C, C++, Java, PHP, and Python.

Method 1: Recursion

Recursion, a powerful concept in programming, can be harnessed to print numbers from 1 to 100 without a loop. In this method, a function calls itself until a base condition is met.

#include <stdio.h>

void printNumbers(int n) {
    if(n <= 100) {
        printf("%d ", n);
        printNumbers(n + 1);
    }
}

int main() {
    printNumbers(1);
    return 0;
}

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
#include <iostream>

void printNumbers(int n) {
    if(n <= 100) {
        std::cout << n << " ";
        printNumbers(n + 1);
    }
}

int main() {
    printNumbers(1);
    return 0;
}

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
<?php
function printNumbers($n) {
    if($n <= 100) {
        echo $n . " ";
        printNumbers($n + 1);
    }
}

printNumbers(1);
?>

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
import java.io.*;  public class PrintNumbers {
    static void printNumbers(int n) {
        if(n <= 100) {
            System.out.print(n + " ");
            printNumbers(n + 1);
        }
    }

    public static void main(String[] args) {
        printNumbers(1);
    }
}

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
def print_numbers(n):
    if n <= 100:
        print(n, end=" ")
        print_numbers(n + 1)

print_numbers(1)


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

Method 2: Generator Function (Python Exclusive)

Python's generator functions offer an elegant solution for generating sequences without explicitly storing them in memory. By utilizing the `yield` keyword, we can create a generator that yields numbers from 1 to 100.

def generate_numbers():
    for i in range(1, 101):
        yield i

for num in generate_numbers():
    print(num, end=" ")

Method 3: Using Array and Array Functions

This method involves creating an array of numbers from 1 to 100 and utilizing array functions to print them sequentially.

#include <stdio.h>

int main() {
    int numbers[100];
    
    // Using array and array functions to initialize the array with numbers 1 to 100
    for(int i = 0; i < 100; i++) {
        numbers[i] = i + 1;
    }

    // Printing the numbers using array iteration
    for(int i = 0; i < 100; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

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
#include <iostream>
#include <array>

int main() {
    // Using std::array and array functions to initialize the array with numbers 1 to 100
    std::array<int, 100> numbers;
    for(int i = 0; i < 100; i++) {
        numbers[i] = i + 1;
    }

    // Printing the numbers using array iteration
    for(int i = 0; i < 100; i++) {
        std::cout << numbers[i] << " ";
    }

    return 0;
}

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
<?php
  $numbers = range(1, 100);
  echo implode(" ", $numbers);
?>

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
import java.io.*;  public class PrintNumbers {
    public static void main(String[] args) {
        // Using array and array functions to initialize the array with numbers 1 to 100
        int[] numbers = new int[100];
        for(int i = 0; i < 100; i++) {
            numbers[i] = i + 1;
        }

        // Printing the numbers using array iteration
        for(int i = 0; i < 100; i++) {
            System.out.print(numbers[i] + " ");
        }
    }
}

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
# Using list comprehension to create a list of numbers from 1 to 100
numbers = [i for i in range(1, 101)]

# Printing the numbers using list iteration
for num in numbers:
    print(num, end=" ")

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

Conclusion

In conclusion, although loops are the conventional choice for iterating through sequences, these alternative methods showcase the versatility and flexibility of programming languages. Whether through recursion, generator functions, or array manipulation, there's always a way to achieve the desired outcome in the world of programming.

       

Advertisements

ads