0

I am an AP Computer Science student and I was wondering how to finish up my factorial code using for loops. Here is what I have so far:

import java.util.Scanner;
public class Factorial 
{

    public static void main(String[] args) 
    {
        int num;
        int factorial = 1;
        int i;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        num = input.nextInt();
        for(i = 1; i <= num; i++)
        {
            factorial *= i;
        }
        System.out.println("!"+num+"="+factorial);

I tested it using eclipse and it worked for all integers until (and including) 12. When I entered 13, it gave me an incorrect number. Can someone explain to me why that is and how to rectify it?

Also, the assignment says I need to print out the numbers that I'm multiplying in addition to the answer (i.e. if num = 5, then the output is 5! = 5*4*3*2*1 = 120). Can someone point me in the right direction for that issue?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rahil Modi
  • 19
  • 2
  • what was the output on 13? – Ayush Gupta Dec 29 '17 at 06:23
  • 3
    `13!` doesn't fit in an `int`. You can use a `long` to get a few more factorials (up to `20!`), but if you want higher numbers you'll need to look into `BigInteger`. – bcsb1001 Dec 29 '17 at 06:24
  • Hi Rahil change Int to BigInt –  Dec 29 '17 at 06:24
  • There are already many factorial examples on SO: https://stackoverflow.com/questions/8183426/factorial-using-recursion-in-java, https://stackoverflow.com/questions/11446973/find-factorial-of-large-numbers-in-java/11447063, https://stackoverflow.com/questions/891031/is-there-a-method-that-calculates-a-factorial-in-java/891073 – tkruse Dec 29 '17 at 06:26
  • Possible duplicate of [Is there a method that calculates a factorial in Java?](https://stackoverflow.com/questions/891031/is-there-a-method-that-calculates-a-factorial-in-java) – tkruse Dec 29 '17 at 06:27

2 Answers2

0

Rahul first change Int to BigInt to fix the issue for numbers after 12 because Int max value is 2,147,483,647. Then change your for loop to below for printing in desired manner.

String a = “”;

for(i = 1; i <= num; i++)
    {
        factorial = factorial.multiply(i);

        if(i==1)
              a = String.valueOf(i)
        else
             a = a + "*" + String.valueOf(i)

    }
    System.out.println("!"+num+"="+ a + "="+factorial);

This prints the numbers as 1*2*3*5... if you want in reverse 5*4*3... then simply change the for loop to for(i=num;i>=1;i--)

  • Is `factorial` supposed to be a BigInteger? Because that doesn't have a `*=` operator. Also, an actual declaration wouldn't harm. – daniu Dec 29 '17 at 06:39
  • @daniu check my edit, i didnt noticed that since that is understandable. –  Dec 29 '17 at 06:42
  • 1
    `*` operator can't be used with `BigInteger`. You have to use `.multiply()` – Lino Dec 29 '17 at 06:50
0

This is because 13's factorial is greater than integer's MAX_VALUE (2147483647). As a result MAX_VALUE is subtracted from supposed result 6227020800 which is greater than MAX_VALUE (4079537153). This result is again subtracted from MAX_VALUE and the result 1932053504 is printed.

The above is only an explanation. As the comment below indicates, in practice the number is not subtracted by the JVM rather it overflows.

Rizwan Ejaz
  • 286
  • 2
  • 9
  • 4
    *is subtracted*, is not entirely correct, the int just overflows – Lino Dec 29 '17 at 06:38
  • used subtracted because i think it gives a better idea of whats going on – Rizwan Ejaz Dec 29 '17 at 06:40
  • 2
    I think when explaining something you should be as correct and direct as possible. Else when someone reading this. he may assume that the compiler subtracts `2147483647` from any integer as long as it is bigger than `2147483647`, which is not how it works – Lino Dec 29 '17 at 06:49
  • explanation added @Lino – Rizwan Ejaz Dec 29 '17 at 07:24