Explanation :
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120.
Recursion means calling a method within itself until it returns a value.
Code:
class Factorial{
static int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-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);
}
}
No comments:
Post a Comment