-2

How I can print my name 100 times without using loops in C#? Any clues? I have already tried to print number 1 to 100 without using loops but how I can do it with string(my name)?

Thank you in advance!

Lina
  • 9
  • 1
  • 3
    It seems like you know about recursion. Can you show your attempt? – Sweeper Feb 20 '20 at 21:23
  • 6
    `Console.WriteLine("name");Console.WriteLine("name");Console.WriteLine("name");Console.WriteLine("name");Console.WriteLine("name");Console.WriteLine("name");` etc... ;) – Broots Waymb Feb 20 '20 at 21:24
  • 1
    well recursion with a counter is in the end just a complicated for loop, so this wont help – Isparia Feb 20 '20 at 21:26
  • 1
    Joke aside, it sounds like you already have it working with numbers... just swap your current `Console.WriteLine(number);` for `Console.WriteLine("name");`. Or is there something specific you're struggling with? – Broots Waymb Feb 20 '20 at 21:28
  • You could do this a bunch of different ways...recursion although recursion is just a loop in my opinion, using an if statement with a count calling functions that call another function with counts which way would you like to see...show us what your doing with the numbers and we can point you in the right direction – Brent Feb 20 '20 at 21:35
  • Console.WriteLine( String.Join( ", ", Array.ConvertAll( Enumerable.Range(1, 100).ToArray(), i => i.ToString() ) ) ); – Lina Feb 20 '20 at 22:00

1 Answers1

9

I guess it comes down to what your definition of loop is.

Recursion

public static void Recurse(int count)
{
   if(count == 0) return;
   Console.WriteLine("Bobo");
   Recurse(count - 1);
}

Fancy pants linq

Enumerable.Range(0, 100)
          .ToList()
          .ForEach(x => Console.WriteLine("bobo"));

or supplied by MKR

Console.Write(string.Concat(Enumerable.Repeat("Robo\n", 100))

Note : this still loops underneath, via an iterator method


I'm a kid from the 80's goto

public static void Goto(int count)
{     
   startLoop:
   if (count-- <= 0)return;
   Console.WriteLine("Bobo");
   goto startLoop;
}

2 finger Copy/Paste loop

Console.WriteLine("Bobo");
Console.WriteLine("Bobo");
Console.WriteLine("Bobo");
Console.WriteLine("Bobo");
...
halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141