Algoritma Pemrograman
Iteratif C++
Rekursif C++
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int fpb(int a, int b){
int r;
while(b>0){
r=a%b;
a=b;
b=r;
}
return(a);
}
int main(int argc, char** argv) {
int a,b;
cin>>a>>b;
cout<<fpb(a,b);
return 0;
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int fpb(int a, int b){
int r;
while(b>0){
r=a%b;
a=b;
b=r;
}
return(a);
}
int main(int argc, char** argv) {
int a,b;
cin>>a>>b;
cout<<fpb(a,b);
return 0;
}
-
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int fpb(int a, int b){
if(b==0) return (a);
if(a<b) return (fpb(b,a));
else return (fpb(a-b,b));
}
int main(int argc, char** argv) {
int a,b;
cin>>a>>b;
cout<<fpb(a,b);
return 0;
}
EmoticonEmoticon