
为了找到给定阶乘中的末尾零,让我们考虑以下三个示例:
示例1
输入 - 4
输出 - 0
解释 - 4! = 24,没有末尾零。
阶乘4! = 4 x 3 x 2 x 1 = 24。末尾零的位置没有数字4。
示例2
输入 - 6
输出 - 1
解释 - 6! = 720,有一个末尾零。
阶乘6! = 6 x 5 x 4 x 3 x 2 x 1 = 720,有一个末尾零,因为末尾零的位置有一个数字0。
示例3
输入如下 -
n = 4 n = 5
输出如下 −
4! 的尾随零的数量为 0
5! 的尾随零的数量为 1
以下是一个用于查找给定阶乘的尾随零的 C 程序 −
在线演示
#include <stdio.h>
static int trailing_Zeroes(int n){
int number = 0;
while (n > 0) {
number += n / 5;
n /= 5;
}
return number;
}
int main(void){
int n;
printf("enter integer1:");
scanf("%d",&n);
printf("</p><p> no: of trailing zeroe's of factorial %d is %d</p><p></p><p> ", n, trailing_Zeroes(n));
printf("enter integer2:");
scanf("%d",&n);
printf("</p><p> no: of trailing zeroe's of factorial %d is %d ", n, trailing_Zeroes(n));
return 0;
}当执行上述程序时,它产生以下结果 −
enter integer1:5 no: of trailing zeroe's of factorial 5 is 1 enter integer2:6 no: of trailing zeroe's of factorial 6 is 1
以上就是给定一个阶乘,编写一个C程序来找到末尾的零的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号