Contact Learn C
Copy

Program 177:To Calculate Permutation value nPr

Program 177:
 
#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
  1. Here we used
    • n → To store Value
    • r → To store Value
    • nPr → To store output
  2. 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
  3.  
    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.
  4. 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.
  5. To understand factorial in detail CLICK HERE.

Output:
Calculate Permutation value nPr




Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide