//idea: remove every right most zero public class RightMostDigitOfFactorial{ public static int findRightMost(long n){ System.out.println("....................."); long rmd = 1; long f = 1; for(long i= 2;i<=n;i++){ long b = i; // ignore all right most zeros from b while(b % 10 == 0) b /= 10; f = f * i; // ignore all right most zeros from f while( f % 10 == 0) f/=10; // don't make f exceed 9999 i.e, keep track // of only 4 right most digits. if( f > 10000) f %= 10000; } return (int) f % 10 ; } }