Possible Duplicate:
Display numbers from 1 to 100 without loops or conditions
Interview question:
Print 1 to 10 without any loop in java.
Possible Duplicate:
Display numbers from 1 to 100 without loops or conditions
Interview question:
Print 1 to 10 without any loop in java.
Simple way: System.out.println
the values:
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);
System.out.println(6);
System.out.println(7);
System.out.println(8);
System.out.println(9);
System.out.println(10);
Complex way: use recursion
public void recursiveMe(int n) {
if(n <= 10) {// 10 is the max limit
System.out.println(n);//print n
recursiveMe(n+1);//call recursiveMe with n=n+1
}
}
recursiveMe(1); // call the function with 1.
If you like your programs obtuse, no loops, condition statements or main method.
static int i = 0;
static {
try {
recurse();
} catch (Throwable t) {
System.exit(0);
}
}
private static void recurse() {
System.out.print(++i + 0 / (i - 11) + " ");
recurse();
}
This uses a loop but may be an interesting answer
Random random = new Random(-6732303926L);
for(int i = 0; i < 10; i++)
System.out.print((1+random.nextInt(10))+" ");
}
You can restructure this to not use a loop.