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

16y 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 program to find the factorial of a given number?

Here's a simple Java program to find the factorial of a given number using a recursive method: import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&quot;Enter a number: &quot;); int number = scanner.nextInt(); System.out.println(&quot;Factorial of &quot; + number + &quot; is &quot; + factorial(number)); } static int factorial(int n) { return (n == 0) ? 1 : n * factorial(n - 1); } } This program prompts the user for a number and calculates its factorial recursively.


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.


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; }


Can we add website link when we write java program?

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


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'


Write a C program to find out factorial if 10?

Here's a simple C program to calculate the factorial of 10: #include &lt;stdio.h&gt; int main() { int i; unsigned long long factorial = 1; // Use unsigned long long for larger results for(i = 1; i &lt;= 10; i++) { factorial *= i; } printf(&quot;Factorial of 10 is %llu\n&quot;, factorial); return 0; } This program uses a loop to multiply numbers from 1 to 10 and stores the result in factorial, which is then printed.


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.


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 java program for finding multiligual languages?

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