+92 332 4229 857 99ProjectIdeas@Gmail.com

Permutation (C++)


Permutation
Permutation is an act of rearranging the values in an ordered fashion. For example, there are 6 permutations of the set {a,b,c}, namely [a,b,c], [a,c,b], [b,a,c], [b,c,a], [c,a,b], and [c,b,a].

Below is the code for calculating total number of permutations.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

long GetFact(long number)
{
long factorial=1;

       for(int i=number;i>0;i--)
              factorial=factorial*i;

return factorial;
}

long nPr(int n,int r)
{
 return GetFact(n)/GetFact(n-r);
}

int main()
{
       cout<<nPr(5,0);cout<<endl;
       cout<<nPr(5,1);cout<<endl;
       cout<<nPr(5,2);cout<<endl;
       cout<<nPr(5,3);cout<<endl;
       cout<<nPr(5,4);cout<<endl;
       cout<<nPr(5,5);cout<<endl;
      
       _getche();
       return 0;
}

Output
1
5
20
60
120
120

Related article

0 comments: