多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# C 程序:计算并打印 nCr 的值 > 原文: [https://beginnersbook.com/2015/02/c-program-to-calculate-and-print-the-value-of-ncr/](https://beginnersbook.com/2015/02/c-program-to-calculate-and-print-the-value-of-ncr/) 在下面的程序中,我们计算并显示 nCr 的值。 **nCr 也可以表示为 `C(n, r)`** 公式为: `C(n, r)= n! /(r!(n - r)!)`,对于`0 <= r <= n`。这里`!`代表阶乘。例如:`C(6, 2)= 6! /(2! * (6-2)!) => 720 /(2 * 24) => 15`。 我们在以下程序中进行了相同的计算。 ```c #include <stdio.h> int fact(int num); void main() { int n, r, ncr_var; printf("Enter the value of n:"); scanf("%d", &n); printf("\nEnter the value of r:"); scanf("%d", &r); /* ncr is also represented as C(n,r), the formula is: * C(n,r) = n! / ( r!(n - r)! ). For 0 <= r <= n. */ ncr_var = fact(n) / (fact(r) * fact(n - r)); printf("\nThe value of C(%d,%d) is: %d",n,r,ncr_var); } /* This function is used to find the * factorial of given number num */ int fact(int num) { int k = 1, i; // factorial of 0 is 1 if (num == 0) { return(k); } else { for (i = 1; i <= num; i++) { k = k * i; } } return(k); } ``` **输出:** ```c Enter the value of n: 5 Enter the value of r: 2 The value of C(6,2) is: 15 ```