# How will you print numbers from 1 to 100 without using loop?

If we take a look at this problem carefully, we can see that the idea of “loop” is to track some counter value e.g. “i=0” till “i <= 100". So if we aren't allowed to use loop, how else can be track something in C language!

Well, one possibility is the use of ‘recursion’ provided we use the terminating condition carefully. Here is a solution that prints numbers using recursion.

```#include <stdio.h>

/* Prints numbers from 1 to n */
void printNos(unsigned int n)
{
if(n > 0)
{
printNos(n-1);
printf("%d ",  n);
}
return;
}

/* Driver program to test printNos */
int main()
{
printNos(100);
getchar();
return 0;
}
```

Time Complexity: O(n)

Now try writing a program that does the same but without any “if” construct.
Hint — use some operator which can be used instead of “if”.

Please note that recursion technique is good but every call to the function creates one “stack-frame” in program stack. So if there’s constraint to the limited memory and we need to print large set of numbers, “recursion” might not be a good idea. So what could be the other alternative?

Another alternative is “goto” statement. Though use of “goto” is not suggestible as a general programming practice as “goto” statement changes the normal program execution sequence yet in some cases, use of “goto” is the best working solution.

So please give a try printing numbers from 1 to 100 with “goto” statement. You can use GfG IDE!

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

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.2 Average Difficulty : 1.2/5.0
Based on 30 vote(s)