Write a program to print numbers from 1 to 100 without using loops!

This is a very popular interview Question in campus interviews. The interviewer actually wants to check your knowledge in recursion by this question.

Method 1 (Using Recursion):

void printUp(int startNumber, int endNumber) {
  if (startNumber > endNumber) return;
  printf("[%d]\n", startNumber++);
  printUp(startNumber, endNumber);
}

 

Method 2 (Using goto):

void printUp(int startNumber, int endNumber) {
  start:
    if (startNumber > endNumber) {
      goto end;
    } else {
      printf("[%d]\n", startNumber++);
      goto start;
    }
  end: return;
}

 

C Programming Questions