Print 1 to 100 in C++, without loop and recursion

C++ProgrammingServer Side Programming

There are several methods to print numbers without using loops like by using recursive function, goto statement and creating a function outside main() function.

Here is an example to print numbers using goto statement in C++ language,

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int count=1;
   int x;
   cout << "Enter the max value of x : ";
   cin >> x;
   PRINT:
   cout << " " << count;
   count++;
   if(count<=x)
   goto PRINT;
   return 0;
}

Output

Enter the max value of x : 1

In the above program, we used GOTO statement to print the numbers from 1 to 100 without using loops and recursion.

PRINT:
cout << " " << count;
count++;
if(count<=x)
goto PRINT;
raja
Published on 06-Nov-2018 06:04:26
Advertisements