import java.io.*; 
class Factorial{
  public static void main(String[] args) {
      try{
        BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter the number");
        int a= Integer.parseInt(object.readLine());
        int fact= 1;
        System.out.println("Factorial of " +a+ ":");
        for (int i= 1; i<=a; i++){
              fact=fact*i;
        }
        System.out.println(fact);
    }
    catch (Exception e){}
  }
}

Trying numbers equal or greater than 17 outputs a negative number. Why?!?!

And not only that, but the answers are only correct up to 12. Inputting 13 or higher gets you wrong answers. Inputting 62 gives you 0. Is this due to the int boundaries?

I am pretty sure it is, but why do I get negative numbers O_O?

Recommended Answers

All 4 Replies

U can use BigIntegers. Here is a solution

public static void main( String[] args )
    {

        try
        {

            BufferedReader object = new BufferedReader( new InputStreamReader( System.in ) );
            System.out.println( "enter the number" );
            int a = Integer.parseInt( object.readLine() );
            BigInteger fact = new BigInteger( "1", 10 );
            System.out.println( "Factorial of " + a + ":" );
            for ( int i = 1; i <= a; i++ )                {
                BigInteger temp = new BigInteger( "" + i + "", 10 );
                fact = fact.multiply( temp );
            }
            System.out.println( fact );
        }

        catch ( Exception e )
        {
        }

    }

The reason you are getting negative numbers is most probably due to overflow i.e due to the capacity of data type int ,
A simple example would be as follows

int a=2147483647;
a+=1;
System.out.println("a = "+a);

The above would print -2147483648 and not 2147483648 as you might have expected.

Also I am guessing thats the issue cause, the maximum number int can hold is 2147483647, and 12!=2147483647 which is less than that, and hence the answers till 12 would be correct, on the other hand 13! is greater than 2147483647 and hence due to the overflow problem you are getting an incorrect answer.

A good way to overcome this problem would be to use the BigInteger class

Also I am guessing thats the issue cause, the maximum number int can hold is 2147483647, and 12!=2147483647 which is less than that, and hence the answers till 12 would be correct, on the other hand 13! is greater than 2147483647 and hence due to the overflow problem you are getting an incorrect answer.

Sorry I meant to say 12! < 2147483647

the answer is obvious . it's beacuse of the Limitation of the Variables in Programming languages. i mean 17! is equal to 355,687,428,096,000 that is bigger than Integer limit. if you want calculate factorial result of the numbers greater than 16 , you should use long int type.

commented: No use BigInt class, then there is no chance of overflow! -3
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.