Kamis, 31 Maret 2016

PEMROGRAMAN FUNGSI GCD ( KASUS 5.8 )

1.C++:
Interatif :
#include <iostream>
using namespace std;
int gcd(int c, int d)
{   int r;
    while (d > 0) {
 r = c % d;
 c = d;
 d = r;  
    }
    return (c);
}

int main(int argc, char** argv) {
 int a,b;
 cout<<"Mausukkan angka : ";cin>>a;
 cout<<"Mausukkan angka lagi : ";cin>>b;
 cout<<"Hasil GCD = "<<gcd(a,b)<<endl;
 


 return 0;
}

 Rekursif :
#include <iostream>
using namespace std;
int gcd(int c, int d)
{   
    if (d==0) return(c);
    if (c<d) return(gcd(d,c));
    return(gcd(c-d, d)); 
}

int main(int argc, char** argv) {
 int a,b;
 cout<<"Mausukkan angka : ";cin>>a;
 cout<<"Mausukkan angka lagi : ";cin>>b;
 cout<<"Hasil GCD = "<<gcd(a,b)<<endl;
  


 return 0;

}



luvne.com ayeey.com cicicookies.com mbepp.com kumpulanrumusnya.com.com tipscantiknya.com

0 komentar:

Posting Komentar