-3

I'm studying java programming by going through Computer Science at the Saylor academy's webpage. I got to the point where I'm learning about debugging programs in NetBeans, and there is a program that computes and prints the factorial of n (=1*2*3*...*n). The text says that there is a logical error in the program but nothing about where it is. And I can't seem to figure out where the logical error is.

Anyone who can help? The program code:

/** Compute the factorial of n */
public class Factorial {
   // Print factorial of n
   public static void main(String[] args) {
      int n = 20;
      int factorial = 1;

      // n! = 1*2*3...*n
      for (int i = 1; i <= n; i++) {
         factorial *= i;
      }
      System.out.println("The Factorial of " + n + " is " + factorial);
   }
}
keenthinker
  • 7,645
  • 2
  • 35
  • 45
user1088537
  • 181
  • 3
  • 13
  • 7
    Have you tried running it? – SJuan76 Jun 04 '14 at 20:24
  • 1
    http://en.wikipedia.org/wiki/Integer_overflow – Pshemo Jun 04 '14 at 20:25
  • 1
    The program would work fine but not for `n=20` because the `int` will overflow. – user432 Jun 04 '14 at 20:25
  • Yes I have tried running it/debugging it. – user1088537 Jun 04 '14 at 20:29
  • 2
    Your question is being downvoted for several reasons. One is that it is openly a homework question, which is fine, but isn't written in a way that will let others in the future learn from the principal of the problem. [See this thread for advice](http://meta.stackoverflow.com/questions/10811/how-do-i-ask-and-answer-homework-questions). Another is that you've posted all of your code, but not the error or unexpected behavior it gives you or why you're having difficulty finding the problem. [This article gives some more advice](https://stackoverflow.com/help/mcve). – Kevin Jun 04 '14 at 20:32

4 Answers4

6

20! is 2,432,902,008,176,640,000 (~ 2*10^18), much larger than the largest possible number that can be stored in an int, which is about 2 billion. This code will overflow and display an incorrect answer.

The correction to be made is to change the declaration of factorial from int to long.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Thanks :) I tried debugging step by step and as soon as I reached i=17 the number got negative. Should have thought of the variable type but just got locked looking at the loop :) – user1088537 Jun 04 '14 at 20:28
  • Actually, 12! is the largest factorial that will fit in an `int`. 13! (6,227,020,800) will overflow, yet the resulting incorrect number is still positive (`1932053504`). – rgettman Jun 04 '14 at 20:30
2

The value of 20! is much bigger than the int type can store.

Andrei Bozantan
  • 3,781
  • 2
  • 30
  • 40
0

Use long instead of int
There might not be any Syntax Errors. But there is some error which is unknown to the compiler. Hence it hand overs this problem to OS and the OS immediately terminates the program. This is usually the case with C. Java was made even more powerful by introducing effective EXCEPTION HANDLING to overcome such type of problems i.e., logical errors.

Vamsee
  • 183
  • 1
  • 8
0

As others have pointed out, the data type is your limitation in this case.

Should you wish to compute large maths, you should use a custom data type specifically suited to represent large numbers.

If you wish to learn about such an implementation, I highly recommend you read Paŭlo Ebermann's incredibly thorough answer to this StackOverflow question.

If you have no interest in how the implementation works but simple want to perform a large integer calculation, you can use Java's BigInteger data type.

Community
  • 1
  • 1
Dan Bechard
  • 5,104
  • 3
  • 34
  • 51