c语言编程1×2×3×……×n所得的数末尾有多少0

尾数为0主要抓住相乘数字中的末尾是5和0的数值,一个5就可以和一个2或4相乘得到一个末尾是0的数,所以只要计算有多少末尾是5或0的数字就好;当然25、125之类的数字又可以分为5×5、5×5×5,所以25要当两个5看待;代码如下
#include <stdio.h>
int main()
{
int n,i,j,sum=0;
printf("请输出一个数:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
j=i;//while语句之前必须给表达式赋初值(注意)
while(j%5==0)
{
sum++;
j/=5;
}
if(i==n)
printf("该相乘数字末尾有%d个0",sum);
}
return 0;
}

猜你喜欢

转载自blog.51cto.com/14233078/2363602