Hi to all...
I've just join and found this amazing website/forum today which is very helpful.

So here's the problem.. I need to find the factorial of an input number, I have a slight problem with my script, when i enter a value which is 11 upwards it doesn't print the exact value what should i add or remove to my script..THANKS!

Here's my script:

import java.util.*;
public class factorial{
public static void main(String[] args) {

int z;
double b= 1;
Scanner x=new Scanner(System.in);

System.out.print("Enter Number : ");
z=x.nextInt();

System.out.println("Factorial : " +z+ ":");
for (int i=1 ; i<=z; i++){
b=b*i;
}
System.out.println(b);
}

}

I suspect that 11 factorial is greater than the maximum value of a Java int (about 2,000,000,000), so you'll have to use a different data type (eg long) to go bigger.

so instead of using
double b=1; -------> long=1; ?

Yes, try it

hey men.. thanks a lot, it's my first time in java.... and its not part of my subjects at school..just wanna learn it and use it in the future thnks

I suspect that 11 factorial is greater than the maximum value of a Java int (about 2,000,000,000), so you'll have to use a different data type (eg long) to go bigger.

You haven't even taken care to look at what he is using for the result of the factorial before commenting on the int capacity. He was using a double. Also try checking it yourself, even with b declared as an int he could easily go upto 16's factorial before causing any problem. And you haven't cared to understand the problem and explain the same to him. Refrain yourself from posting wrong information.

@OP: The problem is caused because you are, (were, infact) using double, not that it gives a wrong answer, it gives it in a format that would have caused you to get confused. I ran the program and here's what I observed.

The factorial of 11 when you declare b as double given by the program is : 3.99168E7, when you declare b as int is 39916800, now let me tell you, if you don't already know, that 3.99168E7 means that 3.99168 * 10^7 now you don't need a super computer to tell that the answer for that is 39916800. The same thing happens with 12, for b as double it gives 4.790016E8 as an answer and b as int gives 479001600. Again they both are the same.

So, in essence, double does not give you the wrong answer but the correct answer in a format that looks diifferent.

Commenting on your program and the capacity of int, the size of int in java is 2,147,483,647 which is more than 16's factorial but less than 17's factorial so you can go upto 16 even if you are using int, no need to switch to long for 11's factorial as wrongly mentioned by JamesCherill. If you need to calculate factorials above 16 you can use long.

You haven't even taken care to look at what he is using for the result of the factorial before commenting on the int capacity.

Whoah man, that's a bit harsh!
Yes, I did look at his code, and I knew he was using double, and I knew the problem was with the display, not the calc, and I admitted it was a guess that 11! was > max int. I just gave him the quickest way to get the output he was looking for - which seems to be what he wanted, judging by his reply.

Yes, I did look at his code, and I knew he was using double, and I knew the problem was with the display, not the calc, and I admitted it was a guess that 11! was > max int

If you already knew he was using double, why then comment on the int at all, where does it come in between ? Which made me think you hadn't looked at his code at all. Cause you were hinting at something that was not even remotely related with the cause of the problem.

I just gave him the quickest way to get the output he was looking for - which seems to be what he wanted, judging by his reply.

Giving ready made solutions or trial and error tricks is not whats expected here, when suggesting a solution, underline the problem to the OP so that he can avoid it in future and then present your solution with the reasons for them. Doesn't this seem to be a much better approach then saying something like "Try this out for a solution and don't ask me why the previous one failed in the first place cause I've got no clue at all"

OK man, we agree to disagree. Have a nice day.
James

Hmmm...., I agree to disagree with you for the fact that you are wrong !!!

I just gave him the quickest way to get the output he was looking for - which seems to be what he wanted, judging by his reply.

I will have to agree with verruckt24.
When you said to change the double to int (or long, I don't remember) your explanation was wrong. So the OP assumed that his code was wrong, which wasn't.
We are not here to give quick solutions but to help others understand and learn

I will have to agree with verruckt24.
We are not here to give quick solutions but to help others understand and learn

OK, I was wrong not to explain fully. I apologise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.