Tuesday, 5 June 2018

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

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):

No comments:

Post a Comment