Q:

C program to find nCr and nPr

0

C program to find nCr and nPr

 

C program to find nCr and nPr, remember, nCr = n!/(r!*(n-r)!) and nPr = n!/(n-r)!.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
 
int main()
{
   int n, r;
   long ncr, npr;
 
   printf("Enter the value of n and r\n");
   scanf("%d%d",&n,&r);
   
   ncr = find_ncr(n, r);
   npr = find_npr(n, r);
   
   printf("%dC%d = %ld\n", n, r, ncr);
   printf("%dP%d = %ld\n", n, r, npr);
 
   return 0;
}

long find_ncr(int n, int r) {
   long result;
   
   result = factorial(n)/(factorial(r)*factorial(n-r));
   
   return result;
}
 
long find_npr(int n, int r) {
   long result;
   
   result = factorial(n)/factorial(n-r);
   
   return result;
}
 
long factorial(int n) {
   int c;
   long result = 1;
 
   for (c = 1; c <= n; c++)
      result = result*c;
 
   return result;
}

output:

Enter the value of n and r

2

3

2C3 = 0

2P3 = 2

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now