“Print 1 to 10 with out using loop” in java is quite a frequently asked interview question for automation testing.
simple answer is to use recursive method, where we can call method to increment the number till a particular number is achieved.
let’s see the implementation –
public class IncrementNumberWithOutLooping{ public static void main(String[] args) { Increment(1); } public static void Increment(int i){ System.out.println(i); i++; if(i<=10){ Increment(i); } } }
OutPut:
1
2
3
4
5
6
7
8
9
10