- 1

Can one write Java code that prints 1 through 100 without using any loop or conditions?

10th Oct 2017, 7:51 PM
Thomas Yakubu Pamah
Thomas Yakubu Pamah - avatar
8 odpowiedzi
+ 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.
10th Oct 2017, 10:21 PM
Edward Fish
Edward Fish - avatar
+ 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");
10th Oct 2017, 9:25 PM
Meharban Singh
Meharban Singh - avatar
+ 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); } }
10th Oct 2017, 11:58 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
@edward nice code
11th Oct 2017, 5:13 AM
Oseni Luthfulahi Olaitan
Oseni Luthfulahi Olaitan - avatar