di karena kan ini tugas kelompok Kasus 5.2,sampai 5.4 di posting oleh kawan klik di sini
selamat mengunjungi.
dan di sini saya akan memposting Kasus 5.5,5.6 dan 5.8.
Kasus 5,5 Buatlah fungsi faktorial secara rekursif untuk mencari n!
program C++
#include <iostream>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void factorial (int n,int x, int hasil){
if (x>n){
cout<<"factorial "<<n<<" adalah "<<hasil<<endl;
}
else{
hasil=hasil*x;
x=x+1;
factorial(n,x,hasil);
}
}
int main(){
int hasil,n,x;
cout<<"masukkan nilai factorial :";
cin>>n;
x=1;
hasil=1;
factorial(n,x,hasil);
return 0;
}
hasilnya
program raptornya
hasil raptor
Kasus 5.6 Buatlah fungsi yang menghitung suku ken dari fibonacci dengan menggunakan cara rekursif.
Program C++
#include <iostream>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void fibonacci (int n, int f1,int f2, int x){
int f3;
if(x>(n-2)){
}
else{
f3=f1+f2;
cout<<f3<<", ";
f1=f2;
f2=f3;
x=x+1;
fibonacci(n,f1,f2,x);
}
}
int main(int argc, char** argv) {
int f1,n,f2,x;
cout<<"masukkan suku ke-n :";
cin>>n;
cout<<"fibonacci suku ke-"<<n<<endl;
x=1;
f1=0;
f2=1;
cout<<f1<<", "<<f2<<", ";
fibonacci(n,f1,f2,x);
return 0;
}
dan ini hasilnya
program raptor
Kasus 5.8 Buatlah algoritma iteratif dan rekursif untuk menghitung gcd dari dua bilangan bulat positif
Program raptor
algoritman:
while (d>0) do
r=c mod d
c=d {menyimpan harga terakhir}
d=r {harga terkhir untuk menghentikan perulangan}
end while
gcd=c
Versi rekursif :
if(d=0) then gcd = c
else if(c<d) then gcd=gcd(d,c)
else gcd=gcd(gd,d)










0 komentar:
Posting Komentar