Java Program To Find The Factorial Of A Number Using Recursion
- November 09, 2020
- by
Java Program To Find The Factorial Of A Number Using Recursion
In this Java Program, we calculate the factorial of the number using recursion.
Factorial of n is the product of all the positive descending integers. It is denoted by n!
For example : 4! = 4 * 3 * 2 * 1 = 24
Logic : fact = fact * i;
Step 1 : Create main method
Step 2 : Create variable factorial and store factorial of 4 in it.
Step 3 : Print the number
Step 4 : Create a static function which returns 1
Step 5 : Create recursion function which calls itself
Program :
public class FactorialDemo2 {
public static void main(String[] args){
int factorial = fact(4);
System.out.println("Factorial of 4 is:"+factorial);
}
static int fact(int n)
{
int output;
if(n==1){
return 1;
}
//Recursion : Function calling itself
output = fact(n-1)*n;
return output;
}
}
Output :
Factorial of 4 is : 24
For execution of the same do visit my YouTube Channel :
https://youtu.be/xGJ2VBxZuRc


0 comments:
Post a Comment