您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

给定一个阶乘,编写一个C程序来找到末尾的零

2024/4/26 19:42:21发布11次查看
为了找到给定阶乘中的末尾零,让我们考虑以下三个示例:
示例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 = 4n = 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("
no: of trailing zeroe's of factorial %d is %d
", n, trailing_zeroes(n)); printf("enter integer2:"); scanf("%d",&n); printf("
no: of trailing zeroe's of factorial %d is %d ", n, trailing_zeroes(n)); return 0;}
输出当执行上述程序时,它产生以下结果 −
enter integer1:5no: of trailing zeroe's of factorial 5 is 1enter integer2:6no: of trailing zeroe's of factorial 6 is 1
以上就是给定一个阶乘,编写一个c程序来找到末尾的零的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product