发现给定阶乘中尾随零的 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP