Introduction
Printing numbers from 1 to n is very simple in Java. We can use loops.
But the challenge here is, can we do it using recursion?
Before we use recursion let us understand basics about recursion.
Just a brief introduction about recursion
Recursion is a programming technique where a function calls itself in order to solve a problem. Function keeps calling itself with smaller instances of the problem until it reaches a base case.
Printing Numbers 1 to n Using Recursion
We can print from 1 to n without using loop in java
Let say we want to print numbers from 1 to 10. You are given n as 10.
Since n=10 , we need to start from 10 to go back to 1
10–>9–>8–>7–>6–>5–>4–>3–>2–>1
Function should call itself with n1 until it reaches 0. Since we want to print from 1 to 10 we should print only we reach the base case.




What happens if we write print statement before printNumbers(n1)?
In such scenario, since we are calling from number from n to 1 and printing it first, order of number printed will be from n to 1




That wraps up our tutorial on Java recursion and printing numbers using recursion.