#include <iostream>
#include <math.h>
#include <limits>
#include <cstdlib>
#include <cstring>
using namespace std;
int kata;
char val [100];
int validasi()
{
do
{
kata=0;
cout<<"\t\t";cin>>val;
for(int i=0; i<strlen(val); i++)
{
if(isdigit(val[i])==0)
kata++;
}
if(kata !=0)
cout<<"\t\tInput salah !\n"<<endl;
}
while(kata!=0);
return 0;
}
int GCD_iteratif(int x, int y){
int a, b, c;
if(x<y) {
a=y;
b=x;
}
else {
a=x;
b=y;
}
c=1;
while(c!=0) {
c=a%b;
a=b;
b=c;
}
return(a);
}
int GCD_rekursif(int a, int b) {
int c;
if(b==0)
return (a);
else {
c = a%b;
return (GCD_rekursif(b,c));
}
}
int main(){
int a,b,pilihan;
cout<<"\tFungsi Rekursif dan Iterative Faktor Persekutuan Terbesar"<<endl;
cout<<"\t========================================================="<<endl;
cout<<"\t\tTekan 1 untuk Faktor Persekutuan Terbesar Rekursif\n\t\tTekan 2 untuk Faktor Persekutuan Terbesar Iterative\n\t\tTekan 3 untuk Berehenti"<<endl;
balik:
cout<<"\n\t\tMasukkan Pilihan : ";
validasi(); pilihan=atoi(val);
switch(pilihan){
case 1 : {
cout<<"\n\t\tFaktor Persekutuan Terbesar Rekursif ";
cout<<"\n\t\tMasukkan bilangan pertama : ";
validasi(); a=atoi(val);
cout<<"\t\tMasukkan bilangan kedua : ";
validasi(); b=atoi(val);
if (a==0||b==0)
cout<<"\t\tNilai harus berupa bilangan bulat positif";
else
cout<<"\t\tFPB dari "<< a <<" and "<< b << " adalah "<< GCD_rekursif(a,b)<< endl;
break;
}
case 2 :{
cout<<"\t\tMasukkan bilangan pertama : ";
validasi(); a=atoi(val);
cout<<"\t\tMasukkan bilangan kedua : ";
validasi(); b=atoi(val);
if (a==0||b==0)
cout<<"\t\tNilai harus berupa bilangan bulat positif";
else
cout<<"\t\tFPB dari "<< a <<" and "<< b << " adalah "<< GCD_iteratif(a,b)<< endl;
break;
}
case 3:{
cout<<"\t\tBye ";
return 0;
}
default :
cout<<"\t\tPilihan salah !";
}
cout<<"\n\t---------------------------------------------------------"<<endl;
system("pause");
goto balik;
}
Home / Pemograman
/ Program mencari persekutuan bilangan terbesar dengan rekursif dan iterative di C++
Subscribe to:
Post Comments
(
Atom
)


0 comments:
Post a Comment