Discussion Forums

New User? Please read instructions!

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

  RSS

thiyagarajan.s
Active Member
Joined:10 months  ago
Posts: 14
27/01/2017 10:06 am  

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


ReplyQuote
admin
Eminent Member Admin
Joined:5 years  ago
Posts: 26
10/02/2017 10:56 am  

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.

Edited: 9 months  ago

ReplyQuote
  
Working

Please Login or Register






Share this website !!!
Facebook Twitter Linkedin