You are on page 1of 1

%Program to find gcd of two numbers.

%Rules.
max(X,Y,R1,R2):- X>Y,R1=X,R2=Y.
max(X,Y,R1,R2):- X<Y,R2=X,R1=Y.
gcd(X,Y):- max(X,Y,R1,R2),Z is R1 mod R2,Z>0,gcd(Z,R2).
gcd(X,Y):- max(X,Y,R1,R2),Z is R1 mod R2,Z=0,write(" is "),write(R2),nl.
gcd(X,Y):- X=:=Y,write(" is "),write(X),nl.
%Query.
?- read(X),read(Y),write("GCD of "),write(X),write(" and "),write(Y),gcd(X,Y).

You might also like