C 语言计算 NCR 阶乘的程序

如何编写 C 语言程序来计算 NCR 阶乘,并附带示例。NCR 阶乘背后的数学公式是 nC = n! / (r! (n-r)!)。

此程序允许用户输入两个正整数。然后,程序将计算给定数字的 NCR 阶乘。

#include <stdio.h>

int Cal_Fact(int);

int main()
{
int n, r, ncr;

printf("\n Please Enter the Values for N and R: \n");
scanf("%d %d", &n, &r);

ncr = Cal_Fact(n) / (Cal_Fact(r) * Cal_Fact(n-r));
printf("\n NCR Factorial of %d and %d = %d", n, r, ncr);

return 0;
}

int Cal_Fact(int Number)
{
int i;
int Factorial = 1;

for (i = 1; i <= Number; i++)
{
Factorial = Factorial * i;
}
return Factorial;
}
C Program to Find NCR Factorial of a Number Example

从上面的 “计算 NCR 阶乘的程序” 代码片段中,我们使用了名为 Cal_Fact 的函数来计算数字的阶乘。接下来,我们调用该函数来计算 n、r 和 n-r 的阶乘。我建议您参考此 “数字阶乘” 文章的 “编程” 部分,以了解阶乘程序。

nC = 6! / (2! (6-2)!)  = 720/ (2 * 24) =  15