How will you print numbers from 1 to 100 without using loop? | Set-2
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!
It can be done in many ways to print numbers using any looping conditions such as for(), while(), do while(). But the same can be done without using loops (using recursive functions, goto statement).
Printing numbers from 1 to 100 using recursive functions has already been discussed in Set-1 . In this post, other two methods have been discussed:
- Using goto statement:
#include <stdio.h>
int
main()
{
int
i = 0;
begin:
i = i + 1;
printf
(
"%d "
, i);
if
(i < 100)
goto
begin;
return
0;
}
chevron_rightfilter_noneOutput:
1 2 3 4 . . . 97 98 99 100
- Using recursive main function:
#include <stdio.h>
int
main()
{
static
int
i = 1;
if
(i <= 100) {
printf
(
"%d "
, i++);
main();
}
return
0;
}
chevron_rightfilter_noneOutput:1 2 3 4 . . . 97 98 99 100
Recommended Posts:
- How will you print numbers from 1 to 100 without using loop?
- Print a pattern without using any loop
- Print 1 to 100 in C++, without loop and recursion
- How to print a number 100 times without using loop and recursion in C?
- Print a character n times without using loop, recursion or goto in C++
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- Print substring of a given string without using any string function and loop in C
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- C Program to print numbers from 1 to N without using semicolon?
- Program to print a pattern of numbers
- Print all n-digit strictly increasing numbers
- Print numbers in sequence using thread synchronization
- Print all increasing sequences of length k from first n natural numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.