example..
enter a number: 5
factorial of 5 is::::
1
2
6
24
120
but this codes only displays 120... what is mistake here??
import java.io.*;
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 )throws Exception
{
int num=0;
BufferedReader keith=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number:");
num=Integer.parseInt(keith.readLine());
System.out.println(factorial(num));
}
}
2007-03-09
13:17:18
·
4 answers
·
asked by
forest
1
in
Computers & Internet
➔ Programming & Design