Idea:
If q is 0, then the greatest common divisor is p.
Otherwise, divide p by q to get the remainder r. The greatest common divisor of p and q is the greatest common divisor of q and r.
Code:
pubic static int gcd(int p, int q){
if(q == 0) return p;
int r = p % q;
return gcd(q, r);
}