Print numbers from 1 to N using recursion in Java.

There are following way to print the numbers from 1 to N using recursion in Java.

  • First call the recursionPrint() method with input m int type.
  • If m is less than 10 then first time print the value of m.
  • Now increment the value of m by 1 inside the parameter of the recursionPrint() function and make call again.
  • If m exceeds 10, then simply terminate the current recursionPrint call.
public class RecursionDemo {
	public static void recursionPrint(int m) {
		if (m <= 10) {
			System.out.println(m);
			recursionPrint(m + 1);
		}
	}

	public static void main(String[] args) {
		recursionPrint(1);
	}
}

Output :-
1
2
3
4
5
6
7
8
9
10