Calculating the number of trailing zeros in a factorial number is done by counting the number of 2s and 5s in the factors of the number. Because 2*5 is equal to 10, and 10 is the last zero in the factorial number.
The factorial of 7=5040, and the number of 0s at the end is 1.
According to our logic, 7!=2*3*4*5*6*7, which has 3 2s and 1 5, so the number of 0s at the end is 1.
#include <iostream> using namespace std; int main() { int n = 45; int count = 0; for (int i = 5; n / i >= 1; i *= 5) count += n / i; cout<<"No of trailing 0s in " << n<< "! is " << count; return 0; }
No of trailing 0s in 24! is 10
The above is the detailed content of C/C++ programming to calculate the number of trailing zeros in the factorial of a number?. For more information, please follow other related articles on the PHP Chinese website!