Finding factorial of a number in Java using Recursion. When the factorial program is run with certain inputs 13 and above, we get incorrect results - negative numbers or results which do not match with the actual factorial of that number. The reason is that the factorial of numbers greater than or equal to 13 is too large for the int data type. If you want to attack factorial with multiple threads, you need to partition the series into units of work. With 2 threads, a simplistic approach is to divide the series in 2. For example 8! One way is to allocate the work in an outer method, passing the range into each thread then multiplying the result.
Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:
Here, 4! is pronounced as '4 factorial', it is also called '4 bang' or '4 shriek'.
The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in java language. Let's see the 2 ways to write the factorial program in java.
- Factorial Program using loop
- Factorial Program using recursion
Factorial Program using loop in java
Let's see the factorial Program using loop in java.
Output:
Factorial Program using recursion in java
Let's see the factorial program in java using recursion.
Output:
Next TopicJava Programs