C program to print first n natural numbers without using any loop (do-while, for, or while). In the first program, we use recursion to achieve the desired output, and in the second, we use goto statement, i.e., without creating any function other than main.
C program using recursion
void print(int, int);
int main()
{
int n;
scanf("%d", &n);
print(1, n);
return 0;
}
void print(int s, int n) {
if (s > n)
return;
printf("%d\n", s);
print(++s, n);
}
Download Numbers without loop program.
Output of program:
C program using goto
int main()
{
int n, c = 1;
scanf("%d", &n); // It is assumed that n >= 1
print: // label
printf("%d\n", c);
c++;
if (c <= n)
goto print;
return 0;
}