Factorial using recursion in java
In this post, you will learn how to find the factorial of a number using recursion in the Java programming language.
The factorial is normally used in different areas of mathematics, including algebra, mathematical analysis, and Combinations and Permutations. The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as -
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
Example of factorial
Suppose we want to get a factorial of 7, then the factorial will be -
7! = 7 X 6 X 5 X 4 X 3 X 2 X 1
5040
Factorial program in Java using recursion
Here is the Java program to find the factorial of a number using the recursion function. Recursion function is a function which is called by itself. If the number equals to the 0, it returns 1. The factorial of a number less than equals to 0 does not exist. If the number is greater than 1, the function calls recursively and finds the factorial of a number.
public class FindFactorial{
static int factorial(int num){
if(num == 0)
return 1;
else
return(num * factorial(num-1));
}
public static void main(String args[]){
int i,fact=1;
int number=4;
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output of the above code:
Factorial of 6 = 720
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number