Program to Find nCr and nPr (Combinations and Permutations)

This is a C++ language program code to find nCr (Combinations) and nPr (Permutations).
Note that the program uses following formulas to find nCr and nPr.


The permutations formula is (P(n,r) = n! / (n - r)!).
The combinations formula is (C(n,r) = n! / r! * (n - r)!).

Here we go! :D

#include <iostream>

using namespace std;

long factorial(int x)
{
    long result = 1;
    for (int c = 1; c <= x; c++)
    {
        result = result * c;
    }
    return result;
}

long find_ncr(int x, int y)
{
    long result;
    result = factorial(x) / (factorial(y)*factorial(x-y));
    return result;
} 
long find_npr(int x,int y)
{
    long result;
    result = factorial(x) / factorial(x-y);
    return result;
}

int main()
{
    int n, r;
    long ncr, npr;
    cout << "================" << endl;
    cout << "Enter the value of N : "; cin >> n;
    cout << "Enter the value of R : "; cin >> r;
    cout << "================" << endl;
    ncr = find_ncr(n,r);
    npr = find_npr(n,r);
    cout << n << "C" << r << " = " << ncr << endl;
    cout << n << "P" << r << " = " << npr << endl;
    return 0;
}
PS: I am using Code::Blocks 16.01
Now, lemme give you my screenshot, so you can see like what I'm typing this code.



And....THIS IS IT!


Horay! We did it! \(^o^)/
And yes! you can modification this code to be something new different.
Thank you! :)

reference: C++ programming

Comments

  1. ada bug tuh wkwkkww int di c++ kan sampe 2147483647, long juga ada yg 32bit ada yg 64bit. coba deh running n nya 2000000, r nya 5. kompleksitas waktunya juga bakalan tinggi kalau dimasukin n nya 2147483647, r nya 5. programnya bisa dibikin lebih singkat kok :3 coba baca di https://en.wikipedia.org/wiki/Integer_(computer_science) sama baca juga di https://books.google.co.id/books?id=3SSTJONEmX0C&pg=PA114&lpg=PA114&dq=kombinasi+permutasi+32+bit+integer&source=bl&ots=uhIFtdkGsy&sig=FxkCKvs8aPNw1PI8a8jeTh06wPE&hl=id&sa=X&ved=0ahUKEwj6zsP2utzPAhXGgI8KHbjTAJ0Q6AEIMzAD#v=onepage&q=kombinasi%20permutasi%2032%20bit%20integer&f=false ....... CMIIW :3

    ReplyDelete

Post a Comment

Popular posts from this blog