answersLogoWhite

0

// Iterative solution

public static final long iterativeFactorial(final long n) {

long factorial = 1;

for (long i = 1; i <= n; i++) {

factorial *= i;

}

return factorial;

}

// Recursive solution

public static final long recursiveFactorial(final long n) {

if (n <= 1) {

return n;

}

return n * recursiveFactorial(n - 1);

}

// Arbitrary length solution - may take a while, but works on any positive number.

public static final BigInteger factorial(final BigInteger n) {

BigInteger factorial = BigInteger.ONE;

for (BigInteger i = BigInteger.ONE; i.compareTo(n) <= 0; i = i.add(BigInteger.ONE)) {

factorial = factorial.multiply(i);

}

return factorial;

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Jntu 2-2 oops through java answers?

write a java program to find factorial using recursive and non recursive


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.


Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


Write a program in java to find factorial of a number?

I suggest to use a for loop, more or less like this (assuming the parameter is "n"): product = 1; for (int i = 1; i &lt;= n; i++) { product *= i; }


What is a factorial loop?

An example in Java, to compute 10!: int factorial = 1; for(int i = 1; i &lt; 11; i++) { factorial *= i; }


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


What is javadoc?

write a java program to display "Welcome Java" and list its execution steps.


How write new line program in java?

\n


How do you write a program to factorial in PHP?

To write a program that calculates the factorial of a number in PHP, you can use a recursive function or an iterative approach. Here’s a simple example using a loop: function factorial($n) { $result = 1; for ($i = 2; $i &lt;= $n; $i++) { $result *= $i; } return $result; } echo factorial(5); // Outputs: 120 This code defines a function that multiplies numbers from 2 up to the given number $n to compute the factorial.


How do you write a java program for finding multiligual languages?

You can use Java's built-in functions to write a code that will find multilingual languages.


How java is 100 percent Internet based?

It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.It isn't. It can also be used to write desktop applications. Java does have many options to program for the Internet, but that is not the only possibility.


Write a java code to draw a circle inside in an ellipse?

write a program draw circle and ellipse by using oval methods in java