x

C program to print number from 1 to 500 without using any loop conditions

C program to print number from 1 to 500 without using any loop conditions:

There are many ways to print numbers without using any loop conditions such as for(), while(), do while(). Few ways are,


1. Using recursive functions

2. Using recursive main functions

3. Using goto statement

4. Using printf statement (But it’s not preferred for printing big numbers)

1. Using recursive functions:

#include<stdio.h>

int recursive(int value)

{

int i;

printf(“%d\n”, value);

i = value + 1;

if (i > 500)

return 0;

recursive(i);

}

int main()

{

recursive(1);


return 0;

}

2. Using recursive main functions:

#include<stdio.h>

int main()

{

static int i = 1;

if (i <= 500)

{

printf(“%d\n”, i++);

main();

}

return 0;

}

3. Using goto statement:

#include<stdio.h>

int main()

{

int i = 0;

Start: i = i + 1;

printf(“%d\n”, i);

if (i < 500)

goto Start;

return 0;

}

4. Using printf statement (But it’s not preferred for printing big numbers):

You can use simple C program using printf() function to print the numbers as many times you want.

 



Like it? Please Spread the word!