Thursday, September 30, 2010
Factorial Calculator & Factorial Test
/* Factorial Calculator */
public class FactorialCalculator
{
//recursive declaration on method factorial
public long factorial(long number)
{
if(number <=1) // test for base case
return 1; //base cases: 0!=1 and 1!=1
else //recursion step
return number*factorial(number-1);
}
//output factorials for values 0-10
public void displayFactorials()
{
//calculate factorials of 0 through 10
for(int counter=0; counter<=10; counter++)
System.out.printf("%d!= %d\n", counter, factorial(counter));
}
}
-----------------------------
/* Factorial Test*/
public class FactorialTest
{
//calculate factorials of 0 - 10
public static void main( String args[])
{
FactorialCalculator factorialCalculator=new FactorialCalculator();
factorialCalculator.displayFactorials();
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment