Thursday, 4 February 2016

Permutations and Combinations (Programming Assignment)

Write functions to calculate factorial, nCr and nPr and using those functions perform a custom computation.

nPr is the n
umber of all permutations of n things, taken r at a time. nCr is the n
umber of all combinations of n things, taken r at a time.


Your task is to write three functions fact( ), ncr( ) and npr( ). The function definitions are given for your reference.

int fact(int n): This function should return the factorial of the parameter n. This function can be used to calculate nCr and nPr.
int npr(int n, int r): The function should return the value nPr where n and r as input parameters.
int ncr(int n, int r): The function should return the value nCr where n and r as input parameters.

You should not write the main function as the main function is prefixed. The functions you write should match the prototype in prefixed code. The output is printed in the main function. So NO output should be printed by any of the functions you write.



Explanation:
Given 4 integers a,b,c,d your program should output (aCb - cPd).


Input: 4 integers a, b, c and d
Output: (aCb - cPd)
Constraints:
For simplicity you can assume that input will be given such that a>=b and c>=d.
1<= a,b,c,d <= 10


Solution-


/*
* This is a prefixed code. 
* This code will be added to your program before compilation
*/

#include<stdio.h> //Write a function for n factorial. int fact(int n); //Write a function calculating nCr int ncr(int n,int r); //Write a function calculating nPr int npr(int n,int r); int main(){ int a,b,c,d,output_ncr,output_npr; scanf("%d %d %d %d",&a,&b,&c,&d); output_ncr=ncr(a,b); output_npr=npr(c,d); printf("%d",(output_ncr-output_npr)); return 0; } int fact(int n){ if(n==1||n==0) return 1; else return n*fact(n-1); } int ncr(int n,int r){ long int a,b,c;int d; a=fact(n); b=fact(r); c=fact(n-r);d=a/(b*c); return d; } int npr(int n,int r){ long int a,b;int d; a=fact(n); b=fact(n-r); d=a/b; return d; }

3 comments: