Algoritma mencari permutasi
Deklarasi |
n,r,permutasi : int |
Deskripsi |
Masukkan ( n ) Masukkan ( r )If (n > r ) Permutasi=fak (n)/fak(n-r) Tulis ( nilai permutasi ) Else if ( n < r ) Tulis ( data tidak valid ) End if End if End |
2. Algoritma mencari kombinasi
Deklarasi |
n,r,kombinasi : int |
Deskripsi |
Masukkan ( n ) Masukkan ( r ) If ( n > r ) Kombinasi=fak(n)/(fak(n-r)*fak (r)) Tulis ( nilai kombinasi ) Else if ( n < r ) Tulis ( data tidak valid ) End if End if End |
Untuk Implementasi dalam Program C++ adalah sebagai berikut :
01 | #include <iostream.h> |
02 | #include <stdio.h> |
03 | #include <conio.h> |
04 | long int fak (long int x); |
05 | int main() |
06 |
07 | { |
08 | int n,r, Prts,pilihan,p; |
09 | char ulang; |
10 | do |
11 | { |
12 |
13 | cout<< "PERHITUNGAN KOMBINASI DAN PERMUTASI" <<endl; |
14 | cout<< "1. KOMBINASI" <<endl; |
15 | cout<< "2. PERMUTASI" <<endl; |
16 | cout<< "3. EXIT" <<endl; |
17 | cout<<endl<<endl; |
18 | cout<< "Pilihan Anda : " ;cin>>pilihan; |
19 | switch (pilihan) |
20 | { |
21 | case 1 : |
22 | cout<< " Program Menghitung Kombinasi(nCr)" ; |
23 | cout<<endl; |
24 | cout<< " Masukkan Nilai n : " ; cin>>n; |
25 | cout<<endl; |
26 | cout<< " Masukkan Nilai r : " ; cin>>r; |
27 | cout<<endl; |
28 | if (n>r){ |
29 | Prts = fak(n)/(fak(n-r)*fak (r)); |
30 | cout<<endl; |
31 | printf( " Kombinasinya adalah: %d" ,Prts); |
32 | cout<<endl; } |
33 | else if (n<r){ |
34 | cout<< "data tak valid" ;} |
35 | cout<<endl ; |
36 | break; |
37 | case 2 : |
38 | cout<< " Program Menghitung Permutasi(nPr)" ; |
39 | cout<<endl; |
40 | cout<< " Masukkan Nilai n : " ; cin>>n; |
41 | cout<<endl; |
42 | cout<< " Masukkan Nilai r : " ; cin>>r; |
43 | cout<<endl; |
44 | if (n>r){ |
45 | Prts = fak (n)/fak(n-r); |
46 | cout<<endl; |
47 |
48 | cout<< " Permutasinya adalah: " <<Prts; |
49 | cout<<endl; } |
50 | else if (n<r){ |
51 | cout<< "data tak valid" ;} |
52 | cout<<endl ; |
53 | break; |
54 | case 3 : |
55 | cout<< "Exit" <<endl; |
56 | break; |
57 | default: |
58 | cout<< "Menu tidak tersedia" <<endl; |
59 | break; |
60 | } |
61 | cout<< "Kembali ke Menu Utama (y/n)?" ;cin>>ulang; |
62 | }while(ulang == 'y' ); |
63 | } |
64 |
65 | long int fak (long int n) |
66 | { |
67 | int f; |
68 | if (n<= 1 ) |
69 | { |
70 | f= 1 ; |
71 | } |
72 | else |
73 | { |
74 | f=n*fak(n -1 ); |
75 | } |
76 | return (f); |
77 | } |