Number of trailing zeros of N [on hold]
Number of trailing zeros of N [on hold] I am writing a method which calculates the number of trailing zeros in a factorial of a given number. For example: 6! = 720 --> 1trailing zero 12! = 479001600 --> 2 trailing zero Here is my code import java.math.BigInteger; public class TrailingZeros { public static void main(String args) { int n = 12; System.out.println(solution(n)); } public static int solution(int n) { // computing factorial BigInteger result = BigInteger.ONE; for (int i = 1; i <= n; i++) { result = result.multiply(new BigInteger(i + "")); } String str = String.valueOf(result); int count = 0; char chars = str.toCharArray(); // counting numbers of trailing zeros for (int i = chars.length - 1; i >= 0; i--) { if (chars[i] != '0') break; count++; } return count; ...
