I am trying to print numbers from 1 to 100 without using loops, using C#. Any clues?
No loops, no conditionals, and no hardcoded literal output, aka "divide and conquer FTW" solution:
class P
{
static int n;
static void P1() { System.Console.WriteLine(++n); }
static void P2() { P1(); P1(); }
static void P4() { P2(); P2(); }
static void P8() { P4(); P4(); }
static void P16() { P8(); P8(); }
static void P32() { P16(); P16(); }
static void P64() { P32(); P32(); }
static void Main() { P64(); P32(); P4(); }
}
Alternative approach:
using System;
class C
{
static int n;
static void P() { Console.WriteLine(++n); }
static void X2(Action a) { a(); a(); }
static void X5(Action a) { X2(a); X2(a); a(); }
static void Main() { X2(() => X5(() => X2(() => X5(P)))); }
}
One more:
Console.WriteLine(
String.Join(
", ",
Array.ConvertAll<int, string>(
Enumerable.Range(1, 100).ToArray(),
i => i.ToString()
)
)
);
class Program
{
static int temp = 0;
public static int a()
{
temp = temp + 1;
if (temp == 100)
{
Console.WriteLine(temp);
return 0;
}
else
Console.WriteLine(temp);
Program.a();
return 0;
}
public static void Main()
{
Program.a();
Console.ReadLine();
}
}
public void Main()
{
printNumber(1);
}
private void printNumber(int x)
{
Console.WriteLine(x.ToString());
if(x<101)
{
x+=1;
printNumber(x);
}
}
This is more or less pseudo code I havent done c# in years, PS running on 1 hour of sleep so i might be wrong.
int i = 0;
public void printNum(j){
if(j > 100){
break;
} else {
print(j);
printNum(j + 1);
}
}
public void main(){
print(i);
printNum(i + 1);
}
static void Main(string[] args)
{
print(0);
}
public static void print(int i)
{
if (i >= 0 && i<=10)
{
i = i + 1;
Console.WriteLine(i + " ");
print(i);
}
}