3.3 faktor persekutuan terbesar
1. masalah
Di berikan 2 integer positif tidak atau bukan nol n & m, pola algoritma untuk mencari pembagi yang paling besar.
2. input dan output
M= integer (input)
N =integer
Gcd= integer (output)
R= integer
D= integer
3. algoritma
Read m,n
While (m <0) do
R ß n mod m
N ß m
End while
Gcd ß m
Write = gcd
4. test data
M= 36 n= 28
N= 28 m=8
R=36 % 28 gcd = 8
R= 8
5. coding
Class gcd {
Friend ostream& operator << (ostream. Gcd&);
Friend istream& operator >> (istream. Gcd&);
Public;
Int hitung gcd (int ,int);
Private ;
Int x,y;
};
Int gcd :: hitung gcd (int ,(int d)
{ int r;
While ( d> 0);
R = c % d;
D= d;
D= r;
}
Return ( r );
}
(istream& operation >> (istream& in, gcd& a){
Cout << “ masukan bilangan petama ;” ;
In >> a.x;
Cout << “ masukan bilangan kedua ; “ ;
In >> a.x;
Return in; }
Ostream& operation << istream& out , gcd& a){
Out << “gcd ‘ << a.c << “.” << a. x << );”;
Out << a.hitung gcd (a.x, a.y);
Return out;
}
Void main (){
Gcd run;
Cin >> run ;
Cout << run ;
Getch ();
}
Tidak ada komentar:
Posting Komentar