File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+ import java .math .BigInteger ;
3+
4+ class Factorial {
5+ public static void main (String [] args ) {
6+ Scanner sc = new Scanner (System .in );
7+ System .out .print ("Enter number: " );
8+ int n = sc .nextInt ();
9+
10+ if (n < 0 ) {
11+ System .out .println ("Factorial not defined for negative numbers" );
12+ }
13+ else if (n <= 5000 ) {
14+ BigInteger fact = BigInteger .ONE ;
15+ for (int i = 1 ; i <= n ; i ++) {
16+ fact = fact .multiply (BigInteger .valueOf (i ));
17+ }
18+ System .out .println ("Factorial = " + fact );
19+ }
20+ else {
21+ System .out .println ("Number too large to display factorial." );
22+ System .out .println ("Digits in " + n + "! = " + digitsInFactorial (n ));
23+ System .out .println ("Trailing zeros in " + n + "! = " + trailingZeros (n ));
24+ }
25+ sc .close ();
26+ }
27+
28+ static int trailingZeros (int n ) {
29+ int count = 0 ;
30+ while (n > 0 ) {
31+ n /= 5 ;
32+ count += n ;
33+ }
34+ return count ;
35+ }
36+
37+ static int digitsInFactorial (int n ) {
38+ double sum = 0 ;
39+ for (int i = 1 ; i <= n ; i ++) {
40+ sum += Math .log10 (i );
41+ }
42+ return (int ) sum + 1 ;
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments