I\'m really struggling with the following problem. Could someone please walk me
ID: 672565 • Letter: I
Question
I'm really struggling with the following problem. Could someone please walk me through how to do a problem like this? I'm still very new to c, and I really only understand the logic per say, but not how to actually write a piece of code. Thank you for your help!!!
Write a c program to find nCr and nPr using functions. nCr is the number of possible combinations that can be obtained by taking a sub-set of items from a larger set, and the order of the items chosen in the sub-set does not matter. Similar to nCr, nPr is the number of sub-sets that can be taken from a larger set. However, the order of the subset now matters.
In this problem, you need to create three functions called find nCr, find nPr, and factorial. Function factorial returns the factorial of the input. Functions find nCr and find cPr will call the function factorial to do the partial calculation. See the sample output as follows:
Explanation / Answer
#include<stdio.h>
factorial(int n) //function 1 which calculates the factorial
{
if(n!=1)
return n*factorial(n-1); //recursive call to generate factorial
}
int findnCr(int n,int r) //function 2 calculates the value of nCr
{
int ncr;
ncr=(factorial(n))/((factorial(r))*factorial(n-r)); //claculating value by calling the factorial function twice
return ncr;
}
findnPr(int n, int r) //function 3 calculates the value of nPr
{
int npr;
npr=(factorial(n))/(factorial(n-r)); //claculating value by calling the factorial function twice
return npr;
}
int main()
{
int n,r,npr,ncr;
printf("enter the value of n: ");
scanf("%d",&n);
printf("enter the value of r: ");
scanf("%d",&r);
ncr=findnCr(n,r); //calling function findnCr
npr=findnPr(n,r); //calling function findnPr
printf(" %dC%d is: %d",n,r,ncr); //printing value returned by function findnCr
printf(" %dP%d is: %d",n,r,npr); //printing value returned by function findnPr
}