發現給定階乘中尾隨零的 C 程式


為了找到給定階乘中的尾隨零,我們考慮以下三個示例,如下所述 -

示例 1

輸入 - 4

輸出 - 0

說明 - 4! = 24,沒有尾隨零。

階乘 4! = 4 x 3 x 2x 1 = 24。沒有尾隨零,即在 0 的位置有 4。

示例 2

輸入 - 6

輸出 - 1

說明 - 6! = 720,一個尾隨零。

階乘 6! = 6 x 5 x 4 x 3 x 2 x 1 = 720,一個尾隨零,因為在 0 的位置有 0。

示例 3

輸入如下 -

n = 4
n = 5

輸出如下 -

4! 的尾隨零數為 0

N0 - 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

更新於: 03-Sep-2021

4K+ 瀏覽

職業生涯一飛沖天

獲得認證,透過完成課程

開始
廣告
© . All rights reserved.