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 0
- declare the jump statement named begin, which increases the value of i by 1 and prints it.
- write an if statement with condition i < 100, inside which call the jump statement begin
Below is the implementation of the above approach:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
begin:
i = i + 1;
cout << i << " ";
if (i < 100) {
goto begin;
}
return 0;
}
// This code is contributed by ShubhamCoder
#include <stdio.h>
int main()
{
int i = 0;
begin:
i = i + 1;
printf("%d ", i);
if (i < 100)
goto begin;
return 0;
}
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 100) {
i = i + 1;
System.out.print(i + " ");
}
}
}
using System;
class GFG {
static public void Main()
{
int i = 0;
begin:
i = i + 1;
Console.Write(" " + i + " ");
if (i < 100) {
goto begin;
}
}
}
// This code is contributed by ShubhamCoder
i = 0
while i < 100:
i = i + 1
print(i, end=' ')
// Javascript
let i = 0;
while (i < 100) { // loop will iterate 100 times
i += 1;
console.log(i, end=' '); // print i and add a space
}
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: O(1)
Auxiliary Space: O(1)
Print numbers from 1 to 100 Using recursive-main
Follow the steps mentioned below to implement the recursive main:
- declare variable i of value 1.
- keep calling the main function till i < 100.
Below is the implementation of the above approach:
// C program to count all pairs from both the
// linked lists whose product is equal to
// a given value
#include <stdio.h>
int main()
{
static int i = 1;
if (i <= 100) {
printf("%d ", i++);
main();
}
return 0;
}
// C++ program to count all pairs from both the
// linked lists whose product is equal to
// a given value
#include <iostream>
using namespace std;
int main()
{
static int i = 1;
if (i <= 100) {
cout << i++ << " ";
main();
}
return 0;
}
// This code is contributed by ShubhamCoder
// Java program to count all pairs from both the
// linked lists whose product is equal to
// a given value
class GFG {
static int i = 1;
public static void main(String[] args)
{
if (i <= 100) {
System.out.printf("%d ", i++);
main(null);
}
}
}
// This code is contributed by Rajput-Ji
# Python3 program to count all pairs from both
# the linked lists whose product is equal to
# a given value
def main(i):
if (i <= 100):
print(i, end=" ")
i = i + 1
main(i)
i = 1
main(i)
# This code is contributed by SoumikMondal
// C# program to count all pairs from both the
// linked lists whose product is equal to
// a given value
using System;
class GFG {
static int i = 1;
public static void Main(String[] args)
{
if (i <= 100) {
Console.Write("{0} ", i++);
Main(null);
}
}
}
// This code is contributed by Rajput-Ji
// Program to count all pairs from both
//the linked lists whose product is equal to
// a given value
function main(i) {
if (i <= 100) {
console.log(i + " ");
i = i + 1;
main(i);
}
}
let i = 1;
main(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
Time complexity: O(1)
Auxiliary Space: O(1)