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;
}

0 comments:

Post a Comment