... System.out.printf("%d! = %d%n", counter,
21               factorial(BigInteger.valueOf(counter)));
22         }
23      }
24   }
0! = 1
1! = 1
2! = 2
3! = 6

…
21! = 51090942171709440000 —— 21! and larger values no longer cause overflow
22! = 1124000727777607680000

…
47! = 258623241511168180642964355153611979969197632389120000000000
48! = 12413915592536072670862289047373375038521486354677760000000000
49! = 608281864034267560872252163321295376887552831379210240000000000
50! = 30414093201713378043612608166064768844377641568960512000000000000

Factorial calculations with a recursive method.

Since BigInteger is not a primitive type, we can’t use the arithmetic, relational and equality operators with BigIntegers; instead, we must use BigInteger methods to perform these tasks. Line ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.