Program to calculate the value of nPr in C Program


Given with the n P r, where P represents Permutation, n represents total numbers and r represents arrangement the task is to calculate the value of nPr.

Permutation is the arrangement of data in a sequence or order. Permutation and combination differs in the sense that permutation is the process of arranging whereas combination is the process of selection of elements from the given set.

Formula for permutation is −

nPr = (n!)/(n-r)!

Example

Input-: n=5 r=2
Output-: 20

Algorithm

Start
Step 1 -> declare function to calculate value of nPr
   int cal_n(int n)
      IF n <=1
         Return 1
End
return n*cal_n(n-1)
Step 2 -> Declare function to calculate the final npr
   int nPr(int n, int r)
      return cal_n(n)/cal_n(n-r)
Step 3 -> In main()
   Declare variables as int n=5, r=2
   Print nPr(n, r)
Stop

Example

#include<stdio.h>
//function to calculate the factorial for npr
int cal_n(int n){
   if (n <= 1)
      return 1;
return n*cal_n(n-1);
}
//function to calculate the final npr
int nPr(int n, int r){
   return cal_n(n)/cal_n(n-r);
}
int main(){
   int n=5, r=2;
   printf("value of %dP%d is %d", n, r, nPr(n, r));
   return 0;
}

Output

value of 5P2 is 20

Updated on: 23-Sep-2019

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements