vb_student
Jun 21st, 2006, 03:54 PM
Hello
i was tring to figure out what the result of factorial for 21 using the following code.
java code
import java.io.*;
/**
*
* @author
*/
public class mywelcome {
public static void main (String[] args) throws IOException
{
BufferedReader userin = new BufferedReader
(new InputStreamReader(System.in));
String inputData;
long N, fact = 1;
System.out.println( "Enter N:" );
inputData = userin.readLine();
N = Integer.parseInt( inputData );
if ( N >= 0 )
{
while ( N > 1 )
{
fact = fact * N;
N = N - 1;
}
System.out.println( "factorial is " + fact );
}
else
{
System.out.println("N must be zero or greater");
}
}
}
when i use the code in the netbeans environment, i get the answer -4249290049419214848
but the following link states the answer should be 51090942171709440000
http://chortle.ccsu.edu/CS151/Notes/chap17/ch17_12.html?factText=N+must+be+an+integer+zero+or+greater&N=20
can you guys tell me why the difference?
i was tring to figure out what the result of factorial for 21 using the following code.
java code
import java.io.*;
/**
*
* @author
*/
public class mywelcome {
public static void main (String[] args) throws IOException
{
BufferedReader userin = new BufferedReader
(new InputStreamReader(System.in));
String inputData;
long N, fact = 1;
System.out.println( "Enter N:" );
inputData = userin.readLine();
N = Integer.parseInt( inputData );
if ( N >= 0 )
{
while ( N > 1 )
{
fact = fact * N;
N = N - 1;
}
System.out.println( "factorial is " + fact );
}
else
{
System.out.println("N must be zero or greater");
}
}
}
when i use the code in the netbeans environment, i get the answer -4249290049419214848
but the following link states the answer should be 51090942171709440000
http://chortle.ccsu.edu/CS151/Notes/chap17/ch17_12.html?factText=N+must+be+an+integer+zero+or+greater&N=20
can you guys tell me why the difference?