- 1
Can one write Java code that prints 1 through 100 without using any loop or conditions?
8 Answers
+ 9
@Luka elegant solution! But if(i!=101) is condition.
I think this code:
public class Program
{
static int i = 1;
public static void main(String[] args) {
try {
int x = 1/(101-i);
System.out.println(i);
i++;
main(args);
}catch(Exception e){
}
}
}
more correctly.
+ 6
Well played @Luka, Well played.
Another Solution:-
System.out.print("1 2 3 4 5 6 7 8 9 10 ...........(i m too lazy to write all) ..... 99 100");
+ 3
You guys and your global variables are way to optimal! đ
We need an uglier solution:
public class Meatball{
public static void main(String[] args){
if (args.length == 0)
args = new String[]{"0"};
int a = Integer.parseInt(args[0]) + 1;
args[0] = String.valueOf(a);
System.out.println(a);
if (a < 100)
main(args);
}
}
+ 1
@edward nice code