This is my code and I do not know why the answer after 12 are incorrect. First i calculate the Factorial of input number then I have to show the least valuable digit.
public class q3411 {
public static int leastValuableDigit(int n) {
for(int i = 0; i < String.valueOf(n).length(); i++) {
int x = (int) Math.floor((n % Math.pow(10, i + 1)) / Math.pow(10, i));
if(x != 0) return x;
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int fon = 1;
for(int i = 1; i <= n; i++) fon *= i;
System.out.println((leastValuableDigit(fon)));
}
}