52.9k views
5 votes
Write two functions called permutation() and combination(), where permutation() computes and returns n!/(n-r)!whereas combination() returns. n!/(n-r)!r!A third function called par_input() gets the input values for n and r. An example of correct program behavior follows: Input: 3 2 Output: The permutation is 6, and the combination is 3.

1 Answer

3 votes

Answer:

Here is the program in C.

#include<stdio.h> // header file used to input output operations

int permutation(int n, int r); // function to computer permutation

int combination(int n, int r); //function to compute combination

int factorial(int number); // function to computer factorial

void par_input(int n, int r); // function take take input value of n and r

int main() //start of main() function body

{

int n,r; // declare n and r variables

par_input(n,r); //calls par_input() function to get values of n and r

}

int permutation(int n, int r) // method to calculate permutation

{

return factorial(n) / factorial(n-r); //formula for permutation

}

int combination(int n, int r) // method to calculate combination

{

return permutation(n, r) / factorial(r); //formula for combination

}

int factorial(int number) // to find factorial of a number

{

int fact = 1;

while(number > 0) //loop continues until value of number gets <=0

{

fact = fact *number;

number--;

}

return fact;

}

void par_input(int n, int r) //function to take input values

{

printf("Enter n: "); //prompts user to enter the value of n

scanf("%d", &n); //reads the value of n from user

printf("Enter r: "); ////prompts user to enter the value of r

scanf("%d", &r); //reads the value of r from user

//calls the combination and permutation methods to display results

printf("The permutation is = %d\\", permutation(n, r));

printf("The Combination is = %d", combination(n, r));

}

Step-by-step explanation:

  • The main() function calls par_input() function. This function asks user to enter values of n and r and then calls permutation() and combination() methods to compute the combination and permutation using these input values. Lets say user enters n=3 and r=2
  • The permutation() function computes and returns permutation using the following formula: n! / (n-r)!
  • factorial(n) / factorial(n-r);
  • This function calls factorial method which calculates the factorial of n and n-r.
  • This function then returns the permutation.
  • 3! / (3-2)! = 3*2*1 / 1! = 6
  • Next the combination() function computes and returns combination using the following formula: n! / r! * (n - r)!
  • permutation(n, r) / factorial(r)
  • This function calls permutation() and factorial() methods which calculate the combination of n and n-r
  • This function then returns the combination.
  • 3! / 2! * (3-2)! = 3*2*1 / 2*1 * 1 = 6 / 2 = 3
  • Factorial function computes and returns the factorial of n, r and n-r.
  • Lets take n! When n=3 So this function computes factorial as follows:
  • while loops checks if number>0 It is true as number is 3
  • It multiplies 3 by fact. fact is initialized to 1 So
  • 1*3 = 3
  • Then it decreases number=3 by 1 so number=2
  • Now while loops checks if number>0 It is true as number is 2
  • It multiplies 2 by fact. Value of fact is now 3 So
  • 3*2 = 6
  • Then it decreases number=2 by 1 so number=1
  • Now while loops checks if number>0 It is true as number is 1
  • It multiplies 1 by fact. Value of fact is now 6 So
  • 6*1 = 6
  • Then it decreases number=1 by 1 so number=0
  • Now while loops checks if number>0 It is false as number is 0
  • So the loop breaks and the value of fact is returned.
  • As fact= 6 So factorial of n is 6.

The output of the program is attached in the screen shot.

Write two functions called permutation() and combination(), where permutation() computes-example-1
User Naresh Chennuri
by
5.7k points