Algoritma Kombinatorial {menampilkan hasil kombinasi n C r = (n!) /(n-r)! . r! } |
Deklarasi : n : integer r : integer int f; |
Deskripsi : if ( n < 0) return 0; int f = 1; while (n >1) f* = n--; return f; if (n < 0 || r < 0 || n < r) return factorial(n)/(factorial(n-r) * factorial(r)); } |
Listing
#include <iostream.h> |
#include <conio.h> |
#include <math.h> |
#include <string.h> |
|
using namespace std; |
int faktorial(int n){ |
if (n<0) return 0; |
int f =1; |
while (n>1) f*=n--; |
return f; |
} |
|
int permutasi(int n, int r){ |
if (n<0 || r<0||n<r) return 0; |
return faktorial(n)/faktorial(n-r); |
} |
|
int kombinasi(int n, int r){ |
if (n<0 || r<0 || n < r) return 0; |
return faktorial(n)/(faktorial(n-r)*faktorial(r)); |
} |
|
int main(int bil_pertama, int bil_kedua){ |
cout<<"\n"; |
cout<<"NUR KAHFI IBRAHIM\n"; |
cout<<"09018238\n"; |
cout<<"\n"; |
cout<<"|||||||||||||||||||||||||||\n"; |
cout<<"| |\n"; |
cout<<"| PERMUTASI DAN KOMBINASI |\n"; |
cout<<"| |\n"; |
cout<<"|||||||||||||||||||||||||||\n"; |
cout<<"\n"; |
cout<<"Masukan bilangan pertama : "; |
cin>>bil_pertama; |
cout<<"\n"; |
cout<<"Masukan bilangan kedua : "; |
cin>>bil_kedua; |
cout<<"\n"; |
cout<<"HASIL PERMUTASI = "<<permutasi(bil_pertama,bil_kedua)<<endl; |
cout<<"HASIL KOMBINASI = "<<kombinasi(bil_pertama,bil_kedua)<<endl; |
getch(); |
} |
Tidak ada komentar:
Posting Komentar