首页 > 文章列表 > 给定一个阶乘,编写一个C程序来找到末尾的零

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

阶乘 末尾
240 2023-09-07

为了找到给定阶乘中的末尾零,让我们考虑以下三个示例:

示例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("

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:5
no: of trailing zeroe's of factorial 5 is 1
enter integer2:6
no: of trailing zeroe's of factorial 6 is 1