C++ program to calculate permutation of number

//C++ program to calculate permutation of number i.e. nPr using single function factorial()
//nPr=n!/(n-r)!

#include <iostream>
using namespace std;
long factorial(int n)
{
    long fact=1;
    int i=n;
    for(i=n;i>=1;i--)
        fact=fact*i;
    return fact;
}

int main()
{
    long f1=1,f2=1,f3=1,perm;
    int n,r;
    cout<<"Enter n and r: ";
    cin>>n>>r;
    perm=factorial(n)/factorial(n-r);
    cout<<"The permutation is: "<<perm;
    return 0;
}

No comments:

Post a Comment