How to write a Sample C Program to Print 1 to 100 without using For Loop, Do While, and While Loop with an example.
Sample C Program to Print 1 to 100 without using Loop
In the example, we are going to write a Program to Print 1 to 100 without using Loops. I suggest you look into the C For Loop, C Do While loop, and C While syntax available on our C tutorial page.
#include<stdio.h>
int print (int number);
int main()
{
int num = 1;
print(num);
return 0;
}
int print (int number)
{
if(number <= 100)
{
printf("%d\t", number);
print(number + 1); // Calling Function recursively
}
}

Within this Program to Print 1 to 100 without loops example, When the compiler reaches to print(num) line in the main() program, then the compiler will immediately jump to the below function:
int print (int number)
Within the function, we used the C If Statement to check whether the number is less than or equal to 100 or not. If the condition returns TRUE, then statements inside the If will be executed.
Within the If block of this C Program, we used the print(number + 1) statement, which will help to call the function Recursively with the updated value. If you miss this statement, then after completing the first line, it will terminate, and the output will be 1. Please refer to the Recursion in C article to understand the recursive function.
Print numbers from 1 to 100 using recursive main
In the above program, we defined a reusable function that recursively calls itself to print numbers from 1 to 100. However, in the example below, we call the main() function recursively to print numbers from 1 to 100.
It does not require an extra function. Here, “static” assigns i to 1 at the beginning. If you miss this word, i becomes 1 on each recursive call, and the program ends up in an infinite loop.
#include <stdio.h>
int main(void)
{
static int i = 1;
if(i <= 100) {
printf("%d ", i++);
main();
}
}
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
C Program to Print numbers from 1 to 100 using a goto statement
In the following example, we declared a jump statement (start). We used the assignment operator to increment i by 1. Next, print i as output. The if statement checks whether i becomes 100; if it is less than 100, the goto statement calls the jump statement, and the process repeats. Refer to the goto statement in c.
#include <stdio.h>
int main()
{
int i = 0;
start:
i += 1;
printf("%d ", i);
if (i < 100)
{
goto start;
}
}
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
Also Read