Factorial Of A Number Using Recursion In Java
Last Updated : Mar 11, 2024
In this article we will show you the solution of factorial of a number using recursion in java, factorial of the a number n is defined as the sum of all positive descending integers, denoted by n!.
Factorial can be calculated using the recursive formula below, in which the recursive calls are made to a multiplicity of all of the numbers below the number which the factorial is computed.
A number N's factorial is indeed a product of any and all natural numbers ranging from 1 to N.
In mathematics, factorial is represented by the symbol!. If we tell factorial of number 5, this same result is:
5!=1×2×3×4×5=120.
Let us first discuss recursion briefly prior to actually moving to the main problem.
Recursion is a problem-solving technique in which a method calls itself. Recursion divides an issue into smaller subproblems and solves them with successive method calls.
It eventually combines this same solution with such subproblems to arrive at the final solution.
The product of all of the numbers from 1 to N seems to be the factorial of a number N.
In this case, we will require the user to enter a value before calculating the factorial besides calling the function recursively.
Recursion is defined as calling the function in itself.
Initially, multiplyNumbers() is named from the main() feature with the argument 6 passed.
Because 6 is greater or equal to 1, it is multiplied by the direct consequence of multiplyNumbers() with 5 (num -1). It is a recursive call because it is called from the same function.
Each recursive call reduces the amount of argument num by one until it is less than one.
There isn't any recursive call when the num is lower than one.
Step By Step Guide On Factorial Of A Number Using Recursion In Java :-
public class TalkersCodefile{ public static void main(String[] args) { int num = 10; long factorial = multiply(num); System.out.println("Factorial of " + num + " = " + factorial); } public static long multiply(int num) { if (num >= 1) return num * multiplyNumber(num - 1); else return 1; } }
- To begin, we will create a class termed TalkersCodeFactorial.
- As a result, as a string argument, a public static void main has been created.
- The next step is to generate an integer number, followed by a long factorial number.
- The following step is to write a static function.
- The if else and statement will be used to describe the series.
Conclusion :-
Recursion is a problem-solving technique in which a method calls itself. Recursion divides an issue into smaller subproblems and solves them with successive method calls.
It eventually combines this same solution with such subproblems to arrive at the final solution.
The product of all of the numbers from 1 to N seems to be the factorial of a number N.
I hope this article on factorial of a number using recursion in java helps you and the steps and method mentioned above are easy to follow and implement.