|
Factorial Implementation in Java |
|
|
In mathematics, the factorial of a natural number n is the product of all positive integers less than or equal to n. This is written as n! and pronounced "n factorial", or colloquially "n shriek" or "n bang". The notation n! was introduced by Christian Kramp in 1808.
This tip shows how to implement factorial function in Java.
public class Factorial
{
// Evaluate n!
public static long factorial( int n )
{
if( n <= 1 ) // base case
return 1;
else
return n * factorial( n - 1 );
}
// Simple test program
public static void main( String [ ] args )
{
for( int i = 1; i <= 10; i++ )
System.out.println( factorial( i ) );
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.