I need to write up factorial up to 23! I can do factorial up to 20! but after that I am lost because the number gets too big. I cannot use BigInteger.
I have to store the 0s from the rightmost digit so example outputs:
10! = 3628800 --> fac=36288, num10 = 2
23! = 2585..976640000 --> fac= 2585..97664, num10 = 4
import java.util.Scanner;
public class Factorial10{
    public static void main(String[] args){
        long fac;       // long: factorial is very large
        long pre_fac;       // to check overflow
        int i, n;
        int num10;
        Scanner sc = new Scanner(System.in);
        System.out.print("n? ");
        n = sc.nextInt();
        // Start from fac = 0! = 1
        for(i= 1, fac= 1L; i<n; i++){
            pre_fac = fac;
            fac *= i;
            // check if overflowed
            if(pre_fac != fac /i){
                System.out.println("Overflowed at " + i + "! = " + fac);
                fac = pre_fac;      // roll back to the previous, unoverflowed
                break;
            }
        }
        System.out.println((i-1) + "! = " + fac + "(fac = , num10 = )");
    }
}
```
 
     
     
     
     
     
    