PERMUTASI DAN KOMBINASI (MATDIS)

#include <iostream>
#include <conio.h>
using namespace std;

class Madis{
      public:
             void pilih();
             void permutasi();
             void kombinasi();
      private:
              int q[100];
              };
      void Madis::pilih(){
           int n;
           cout<<"\n1. Permutasi\n2. Kombinasi\n";
           cout<<"\n\npilihan anda :";
           cin>>n;
           if(n==1)permutasi();
           if(n==2)kombinasi();
           else cout<<"\n\n**selesai***\n\n";
           }
      void Madis::permutasi(){
           int n,N,k,K,p;
           N=1;
           K=1;
           cout<<"\nMasukan nilai n = ";
           cin>>n;
           cout<<"masukkan nilai r = ";
           cin>>k;
           if(k>n){
                   cout<<"\nNilai r harus kurang dari n";
                   permutasi();
                   }
           p=n-k;
           for(int i=1;i<=n;i++){
                   N=N*i;
                   }
           for(int a=1;a<=p;a++){
               K=K*a;
               }
           cout<<"\Maka hasi Permutasi : "<<N/K;
           cout<<"\n\n";
           pilih();
           }
      void Madis :: kombinasi(){
           int n,N=1,k,K=1,p,P=1;
           cout<<"\nMasukkan nilai n = ";
           cin>>n;
           cout<<"\nMasukkan nilai r = ";
           cin>>k;
            if(k>n){
                    cout<<"\Nilai r harus kurang dari n";
                    permutasi();
                    }
            p=n-k;
            for(int b=1;b<=n;b++){
                   N=N*b;
                   }
             for(int c=1;c<=k;c++){
                   K=K*c;
                   }
             for(int d=1;d<=p;d++){
                     P=P*d;
                     } 
            cout<<"\n(n-r)! -> "<<P;
            cout<<"\nmaka hasil kombinasi : "<<N/(K*P);
            cout<<"\n\n";
            pilih();
            }
      int main(){
           Madis z;
           z.pilih();
           z.permutasi();
           z.kombinasi();
           system("PAUSE");
           return EXIT_SUCCESS;
           }

Komentar