“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, 10);
}
public static void Increment(int incrementCount, int incrementUpTo){
System.out.println(incrementCount);
incrementCount++;
if(incrementCount<=incrementUpTo){
Increment(incrementCount);
}
}
}
OutPut:
1
2
3
4
5
6
7
8
9
10