In this example, we learn how to print 1 to 100 without using loop in c#. And we’ll solve the problem with two way.
Solution 1: Using C# Linq
1 2 3 4 5 6 7 8 9 10 11 12 |
class Program { public static void Main() { List<int> numbers = Enumerable.Range(1, 100).ToList(); numbers.ForEach(e => Console.Write(e + " ")); Console.ReadLine(); } } |
Soution 2: Using Recursive Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Program { public static void PrintNext(int minNumber, int maxNumber) { if (minNumber <= maxNumber) { Console.Write(minNumber + " "); PrintNext(minNumber + 1, maxNumber); } } public static void Main() { int minNumber = 1; int maxNumber = 100; PrintNext(minNumber,maxNumber); Console.ReadLine(); } } |
Output: