Java calculating factorial using recursive function
The factorial of a given positive integer is the product of all positive integers less than and equal to the given positive integer. For example, the factorial of 5!=1*2*3*4*5=120
Here is a Java program using recursive funcation call to calculate the factoral of a number.
[code language=”java”]
public class Factorial {
public static int factorial(int num)
{
if(num==1)
return num;
return num*factorial(num-1);
}
public static void main(String args[])
{
int size=10;
for(int i=1; i<size; i++)
System.out.format("The factorial of %s is %s\n",i, Factorial.factorial(i));
}
}
[/code]
Output:
[code language=”text”]
The factorial of 1 is 1
The factorial of 2 is 2
The factorial of 3 is 6
The factorial of 4 is 24
The factorial of 5 is 120
The factorial of 6 is 720
The factorial of 7 is 5040
The factorial of 8 is 40320
The factorial of 9 is 362880
[/code]
Search within Codexpedia
Search the entire web