Note :This post is first published on Mar-2013 in my previous blog Techkindle. Moving the content here.

In some interviews we may get a question like print certain numbers without using any loops.There are many approaches to accomplish this task. Here i am giving two of the approaches

  1. With recursion
  2. With goto statement

With Recursion

[sourcecode language="csharp"] void PrintWithRecursion(int fromNumber, int toNumber) { if (fromNumber <= toNumber) { Console.Write(fromNumber + " "); fromNumber++; PrintWithRecursion(fromNumber, toNumber); } } [/sourcecode]

With goto statement

[sourcecode language="csharp"] void PrintWithGoto(int fromNumber, int toNumber) { Repeat: if (fromNumber <= toNumber) { Console.Write(fromNumber + " "); fromNumber++; goto Repeat; } Console.Read(); } [/sourcecode]

Complete Source code:

https://gist.github.com/gopigujjula/5412470

output:

Happy Learning :)