Factorial in Groovy, Java
Example for versions
Groovy 1.7,
Sun Java 6,
gcj 3.4.5
This example uses iterative calculation of factorial and illustrates the use of built-in class BigInteger
which allows to handle arbitrarily large integer numbers.
import java.math.BigInteger;
public class Factorial {
public static void main(String[] args)
{
BigInteger f = BigInteger.ONE;
System.out.println("0! = " + f);
for (int n=1; n<=16; n++)
{ f = f.multiply(BigInteger.valueOf(n));
System.out.println( n + "! = " + f);
}
}
}
Comments
]]>blog comments powered by Disqus
]]>