Examples of C part 47 calculate permutation value nPr
Program:
Output:
Enter value of n in nPr
9
Enter value of r in nPr
9
Value of 9P9=362880.000000Explanation:
Formula for permutation us nPr=n!/(n-r)! under condition of n>=r,n>=0,r>=0
#include<stdio.h> int factorial(int); main() { int n,r; float nPr; printf("Enter value of n in nPr\n"); scanf("%d",&n); printf("Enter value of r in nPr\n"); scanf("%d",&r); if(n>=r&&n>=0&&r>=0) { nPr=(float)factorial(n)/factorial(n-r); printf("Value of %dP%d=%f\n",n,r,nPr); } else { printf("Invalid Entry\n"); } } int factorial(int num) { int i,fact=1; for(i=1;i<=num;i++) { fact=fact*i; } return(fact); }
Enter value of n in nPr
9
Enter value of r in nPr
9
Value of 9P9=362880.000000Explanation:
Formula for permutation us nPr=n!/(n-r)! under condition of n>=r,n>=0,r>=0
int factorial(int); → function Declaration
- Here we used
- n → To store Value
- r → To store Value
- nPr → To store output
printf("Enter value of n in nPr\n"); scanf("%d",&n); printf("Enter value of r in nPr\n"); scanf("%d",&r);
Takes values of n and rif(n>=r&&n>=0&&r>=0) { nPr=(float)factorial(n)/factorial(n-r); printf("Value of %dP%d=%f\n",n,r,nPr); } else { printf("Invalid Entry\n"); }
Checks the required condition as mentioned in the above formula.If the condition is satisfied then if part is executed otherwise the else part.nPr=(float)factorial(n)/factorial(n-r); printf("Value of %dP%d=%f\n",n,r,nPr);
As per formula we need to get factorial of 'n' and 'n-r'.factorial (n) gets the factorial of n same as factorial(n-r).As the factorial(n) send the value of n to factorial function .int factorial(int num) { int i,fact=1; for(i=1;i<=num;i++) { fact=fact*i; } return(fact); }
After getting the value from factorial it calculates formula and store in nPr.- To understand factorial in detail CLICK HERE to Open in same window.