0% found this document useful (0 votes)
3 views1 page

Java Program for Factorial Calculation

This Java program calculates the factorial of a given non-negative integer using a recursive method. It includes error handling for negative inputs by throwing an exception. The main method reads an integer from user input and prints its factorial.

Uploaded by

vineshbaghel10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Java Program for Factorial Calculation

This Java program calculates the factorial of a given non-negative integer using a recursive method. It includes error handling for negative inputs by throwing an exception. The main method reads an integer from user input and prints its factorial.

Uploaded by

vineshbaghel10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

Scanner ;
public class Problem1 {
public static long factorial(int n) {
if (n < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative
numbers.");
}
return (n == 0 || n == 1) ? 1 : n * factorial(n - 1);
}

public static void main(String[] args) {


Scanner scn = new Scanner([Link]);
int num = [Link]();
[Link]("Factorial of " + num + " is: " + factorial(num));

}
}

You might also like