Saturday, December 5, 2009

A program to print numbers from 1 to 100 without using loops

Method1 (Using recursion)


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




Method2 (Using goto)


void printUp(int startNumber, int endNumber)
{
start:

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

end:
    return;
}

No comments:

Post a Comment