Saturday, January 31, 2009

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
{
public static long factorial( int n )
{
if( n <= 1 )
return 1;
else
return n * factorial( n - 1 );
}

public static void main( String [ ] args )
{
for( int i = 1; i <= 10; i++ )
System.out.println( factorial( i ) );
}
}

No comments:

Computers Add to Technorati Favorites Programming Blogs - BlogCatalog Blog Directory