Answers for "how to perform factorial in java"

1

factorial number in java

int num1 = 6; 
 int num2 = 1;
    for(int a = 1; a<=num1; a++) {
        num2 = num2 * a;
    }
        System.out.println(num2);
Posted by: Guest on June-04-2021
0

factorial method library in java

2.4. Factorial Using Apache Commons Math
Apache Commons Math has a CombinatoricsUtils class with a static factorial method that we can use to calculate the factorial.

To include Apache Commons Math, we'll add the commons-math3 dependency into our pom:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.6.1</version>
</dependency>
Let's see an example using the CombinatoricsUtils class:

public long factorialUsingApacheCommons(int n) {
    return CombinatoricsUtils.factorial(n);
}
Posted by: Guest on January-23-2021

Code answers related to "how to perform factorial in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language