Here’s a Java program to find the factorial of a number using recursion:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
int num;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
num = scanner.nextInt();
long factorial = calculateFactorial(num);
System.out.println("Factorial of " + num + " = " + factorial);
}
public static long calculateFactorial(int num) {
if (num == 0 || num == 1) {
return 1;
} else {
return num * calculateFactorial(num - 1);
}
}
}
In this program, we first take user input for the number whose factorial is to be calculated. We then call the `calculateFactorial()` method to calculate the factorial, passing the number as an argument.
The `calculateFactorial()` method is a recursive method that takes an integer parameter `num` and returns the factorial of that number. If `num` is 0 or 1, then the factorial is 1. Otherwise, we recursively call the `calculateFactorial()` method with `num-1` as the argument and multiply the result by `num` to get the factorial.
At the end of the program, we print out the calculated factorial.