var a,b,UCLN,BCNN:integer; begin write(‘Nhap vao 2 so a va b’); readln(a,b); BCNN:=a*b; While a<>b do If a>b then a:=a-b else b:=b-a; UCLN:=a; BCNN:=BCNN div UCLN; write(UCLN,’ ‘,BCNN); end.
Cách 2:Thuật toán Euclide
var a,b,UCLN, BCNN, temp:integer; {temp là biến tạm} begin write(‘Nhap vao so a: ‘); readln(a); write(‘Nhap vao so b: ‘); readln(b); BCNN:= a*b; temp:= b mod a; while temp<>0 do begin temp:= a mod b; a:=b; b:=temp; end; UCLN:=a; BCNN:= BCNN div UCLN; write(‘UCLN = ‘,UCLN,’ BCNN = ‘,BCNN); readln; end.
program ct;
uses crt;
var a,b,UCLN:longint;
begin clrscr;
write(‘Nhap 2 so a va b: ‘); readln(a,b);
while a<>b do
if a>b then a:=a-b
else b:=b-a;
UCLN:=a;
writeln(‘Uoc chung lon nhat cua hai so la: ‘,UCLN);
readln;
end.
Học tốt!
Cách 1:
var a,b,UCLN,BCNN:integer;
begin
write(‘Nhap vao 2 so a va b’);
readln(a,b);
BCNN:=a*b;
While a<>b do If a>b then a:=a-b else b:=b-a;
UCLN:=a;
BCNN:=BCNN div UCLN;
write(UCLN,’ ‘,BCNN);
end.
Cách 2:Thuật toán Euclide
var a,b,UCLN, BCNN, temp:integer; {temp là biến tạm}
begin
write(‘Nhap vao so a: ‘);
readln(a);
write(‘Nhap vao so b: ‘);
readln(b);
BCNN:= a*b;
temp:= b mod a;
while temp<>0 do
begin
temp:= a mod b;
a:=b;
b:=temp;
end;
UCLN:=a;
BCNN:= BCNN div UCLN;
write(‘UCLN = ‘,UCLN,’ BCNN = ‘,BCNN);
readln;
end.