在下面的程序中,我们计算并显示 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。
我们在以下程序中进行了相同的计算。
#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);}输出:
Enter the value of n:5 Enter the value of r:2The value of C(6,2) is: 15
