Write a Java Program to accept a number from the user and calculate the factorial of it.
Program : (Write a Java Program to accept a number from the user and calculate the factorial of it.)
import java.util.Scanner;
public class Factorial {
// Recursive method to calculate factorial
static long calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * calculateFactorial(n - 1);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a non-negative integer to calculate its factorial: ");
int num = scanner.nextInt();
if (num < 0) {
System.out.println("Factorial is not defined for negative numbers.");
} else {
long factorial = calculateFactorial(num);
System.out.println("Factorial of " + num + " is: " + factorial);
}
scanner.close();
}
}
Comments
Post a Comment