NPTEL(WEEK-3)

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



CODING :-

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

//Write a program to calculate nCr and nPr for given numbers.

#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){
    return 1;
}
return n*fact(n-1);

}

int ncr(int n,int r){
int up,down;
up=fact(n);
down=fact(n-r);
down=down*fact(r);
return (up/down);
}


int npr(int n,int r){
int up,down;
up=fact(n);
down=fact(n-r);
return (up/down);
}

No comments:

Post a Comment

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...