How to write a c program to calculate nCr using function in C programming ?
Solution:
#include <stdio.h>
int factorial(int n);
int main()
{
int n, r, ans;
printf("Enter n: ");
scanf("%d", &n);
printf("Enter r: ");
scanf("%d", &r);
ans = factorial(n) / (factorial(n - r) * factorial(r));
printf("nCr = %d\n", ans);
return 0;
}
int factorial(int n)
{
int fact = 1;
int i;
for(i = 1; i <= n; i++)
fact *= i;
return fact;
}