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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With