Thursday, September 27, 2012

Cool Hack,To print Series of numbers without LOOP

Hello friends today i am posting this new code which is very interesting .C program to print first n natural numbers without using any loop(do while, for or while). In the first program we will 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 programming code using recursion


#include <stdio.h>
 
void print(int);
 
int main()
{
  int n;
 
  scanf("%d", &n);
 
  print(n);
 
  return 0;
}
 
void print(int n)
{
  static int c = 1;
 
  if (c == n+1)
    return;
 
  printf("%d\n", c);
  c++;
  print(n);
}

OUTPUT OF ABOVE CODE:

No comments:

Post a Comment