|
-
Jun 21st, 2006, 03:54 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] different answer
Hello
i was tring to figure out what the result of factorial for 21 using the following code.
java code
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/...r+greater&N=20
can you guys tell me why the difference?
Last edited by vb_student; Jun 23rd, 2006 at 06:05 PM.
-
Jun 21st, 2006, 07:14 PM
#2
Frenzied Member
Re: different answer
I don't agree with what how you're doing it. Simply use a for loop:
Code:
long total = 1;
for (int index=21; index >=1; --index)
{
total *=index;
}
Something along those lines... You can use recursion, but I'd prefer this method.
-
Jun 22nd, 2006, 06:14 AM
#3
Re: different answer
That's JavaScript, it's totally different than java besides "51090942171709440000" is very big to contain in a "long" variable,
try this:
Code:
import java.io.* ;
/**
*
* @author Moyeen
*/
public class My
{
public static void main (String[] args) throws IOException {
BufferedReader userin = new BufferedReader(new InputStreamReader(System.in)) ;
String inputData ;
int N;
double fact = 1 ;
System.out.println("Enter N:") ;
inputData = userin.readLine() ;
N = Integer.parseInt(inputData) ;
if (N >= 0) {
while (N > 1) {
fact = fact * N ;
N-- ;
}
System.out.println("factorial is " + fact) ;
}
else {
System.out.println("N must be zero or greater") ;
}
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 23rd, 2006, 01:22 PM
#4
Thread Starter
Frenzied Member
Re: different answer
thanks for the replies
i have just started on java, and am going through this tutorial on central conneticut state university's web tutorial on java
-
Jun 23rd, 2006, 03:58 PM
#5
Re: different answer
Good, welcome to java 
if you are done, please mark the thread resolved
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 23rd, 2006, 06:05 PM
#6
Thread Starter
Frenzied Member
-
Jun 23rd, 2006, 07:08 PM
#7
Re: different answer
this is not how you mark a thread resolved!
Click on "Thread Tools" and select "Mark Thread Resolved"
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 24th, 2006, 04:06 AM
#8
Re: different answer
double is potentially inaccurate. Use java.math.BigInteger instead. Note that you have to use the class's member functions instead of normal mathematical operators, though.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jun 24th, 2006, 11:08 AM
#9
Thread Starter
Frenzied Member
Re: [RESOLVED] different answer
done
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|