Program 177:
Formula for permutation us nPr=n!/(n-r)! under condition of n>=r,n>=0,r>=0
Output:
#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); }Explanation:
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 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"); }
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.
Output: