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
}


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 1 to 100 without loop using Goto and Recursive-main

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.

Similar Reads

Print numbers from 1 to 100 Using Goto statement

Follow the steps mentioned below to implement the goto statement:...

Print numbers from 1 to 100 Using recursive-main

...