I am trying to print numbers from 1 to 100 without using loops, using C#. Any clues?
A completely unnecessary method:
int i = 1;
System.Timers.Timer t = new System.Timers.Timer(1);
t.Elapsed += new ElapsedEventHandler(
(sender, e) => { if (i > 100) t.Enabled = false; else Console.WriteLine(i++); });
t.Enabled = true;
Thread.Sleep(110);
I can think of two ways. One of them involves about 100 lines of code!
There's another way to reuse a bit of code several times without using a while/for loop...
Hint: Make a function that prints the numbers from 1 to N. It should be easy to make it work for N = 1. Then think about how to make it work for N = 2.
Feeling a bit naughty posting this:
private static void Main()
{
AppDomain.CurrentDomain.FirstChanceException += (s, e) =>
{
var frames = new StackTrace().GetFrames();
Console.Write($"{frames.Length - 2} ");
var frame = frames[101];
};
throw new Exception();
}
PrintNum(1);
private void PrintNum(int i)
{
Console.WriteLine("{0}", i);
if(i < 100)
{
PrintNum(i+1);
}
}