Home > Algorithms > Factorial of a given number

Factorial of a given number

December 16, 2011

Formulae for getting factorial of a number is

n! = n * (n-1)!


public class Factorial
{
private long factorial(long n)
{
if(n == 1)
return 1;
else
return n * factorial(n - 1);
}

public static void main(String[] args)
{
Factorial fact = new Factorial();
System.out.println(fact.factorial(5));
}
}
Advertisements
Categories: Algorithms
%d bloggers like this: