You can print numbers from 1 to N without using loops in Java by utilizing recursion. Here's a simple Java program to achieve this:
public class PrintNumbers {
public static void printNumbers(int n) {
if (n <= 0) {
return;
}
printNumbers(n - 1);
System.out.println(n);
}
public static void main(String[] args) {
int n = 10; // Change the value of n as needed
printNumbers(n);
}
}
In this program:
- The
printNumbers
method is a recursive function that prints numbers from 1 to N. - It takes an integer
n
as input. - If
n
becomes less than or equal to 0, the recursion stops. - Otherwise, it recursively calls itself with
n - 1
, printing the numbers in descending order. - After the recursive call, it prints the current value of
n
. - In the
main
method, you can change the value ofn
to specify the range of numbers to be printed.