CONTOH PROGRAM PERMUTASI DAN KOMBINASI
#include <cstdlib>
#include <iostream>
using namespace std;
class Madis{ //deklarasi kelas
public:
void pilih(); //pendeklarasian fungsi pilihan
void permutasi(); //pendeklarasian fungsi permutasi
void kombinasi(); //pendeklarasian fungsi kombinasi
private:
int q[100];
};
int fak(int x){
if(x==1) return x;
else return x*fak(x-1); }
void Madis::pilih(){
int n;
cout<<"\n1. Permutasi\n2. kombinasi";
cout<<"\n\npilihan anda : ";
cin>>n;
if(n==1)permutasi();
if(n==2)kombinasi();
else cout<<"\n\n***selesai***\n\n";
}
void Madis::permutasi(){
// system("cls");
int n,N,k,K,p;
cout<<"\nMasukkan nilai n=";cin>>n;
cout<<"Masukkan nilai r=";cin>>k;
if(k>n){cout<<"\nNilai r harus kutang dari n";
permutasi();}
p = n - k;
N = fak(n);
K = fak(p);
// [1]
cout<<"\nMaka hasil permutasi : "<<N/K;
cout<<"\n\n";pilih();
}
void Madis::kombinasi(){
// system("cls");
int n,N=1,k,K=1,p,P=1;
cout<<"\nMasukkan nilai n=";cin>>n;
cout<<"Masukkan nilai r=";cin>>k;
if(k>n){cout<<"\nNilai r harus kutang dari n";
permutasi();}
p = n-k;
N = fak(n);
K = fak(k);
cout<<"\n(n-r)!-> ";
P = fak(p);
// [2]
cout<<"\nMaka hasil kombinasi : "<<N/(K*P);
// [3]
cout<<"\n\n";pilih();
}
int main(int argc, char *argv[]) //fungsi main
{
Madis z;
z.pilih();
system("PAUSE");
return EXIT_SUCCESS;
}
sama sama gan.
ReplyDelete