Print 1 to 100 without loop using Goto and Recursive-main
Last Updated :
06 Apr, 2023
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:
C++
#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
C
#include <stdio.h>
int main()
{
int i = 0;
begin:
i = i + 1;
printf("%d ", i);
if (i < 100)
goto begin;
return 0;
}
Java
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 100) {
i = i + 1;
System.out.print(i + " ");
}
}
}
C#
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
Python3
i = 0
while i < 100:
i = i + 1
print(i, end=' ')
JavaScript
// Javascript
let i = 0;
while (i < 100) { // loop will iterate 100 times
i += 1;
console.log(i, end=' '); // print i and add a space
}
Output1 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
// 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++
// 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
// 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
# 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#
// 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
JavaScript
// 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);
Output1 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)
Similar Reads
Print 1 to 100 in C++ Without Loops and Recursion We can print 1 to 100 without using loops and recursion using three approaches discussed below: 1) Template Metaprogramming: Templates in C++ allow non-datatypes also as parameters. Non-datatype means a value, not a datatype. Example: CPP // CPP Program to print 1 to 100 // without loops and recursi
3 min read
How to print a number 100 times without using loop and recursion in C? It is possible to solve this problem using loop or a recursion method but what if both are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use #define directive (Macro expansion) CPP // CPP program to print "1" 100 times. // Prints
1 min read
How to print N times without using loops or recursion ? How to print "Hello" N times (where N is user input) without using loop or recursion or goto. Input : N, that represent the number of times you want to print the statement. Output : Statement for N times First, we create a class. After that, we need to initialize the constructor of the class by writ
1 min read
Print a character n times without using loop, recursion or goto in C++ Given a character c and a number n, print the character c, n times. We are not allowed to use loop, recursion, and goto. Examples : Input : n = 10, c = 'a'Output : aaaaaaaaaa Input : n = 6, character = '@'Output : @@@@@@ In C++, there is a way to initialize a string with a value. It can be used to p
2 min read
Print a number 100 times without using loop, recursion and macro expansion in C? It is possible to solve this problem using loop or a recursion method. And we have already seen the solution using #define directive (Macro expansion) but what if all three are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use concept of C
1 min read