I am a beginner programmer in Java. Just started learning the past few weeks.(adsbygoogle = window.adsbygoogle || []).push({});

I'm taking a class and need to create a method that finds the greatest common divisor of two integers. I can assume that both are positive, but I cannot use the Euclidean Algorithm.

I'm sort of lost, but I think I'm on the right track, although this may look confusing:

And then I basically copied the code with an else statement if c > a.Code (Text):

public static int gcd(int a, int c)

{

int gcd;

int attempt;

if (a > c)

{

if (a % c == 0)

{

gcd = c;

return gcd;

}

else

{

for (attempt = c; attempt = 1; attempt--)

do

{

if (c % attempt == 0 && a % attempt == 0)

{

gcd = attempt;

return attempt;

}

}

while (c % attempt != 0 || a % attempt == 0);

}

}

I'm sure there are a lot of mistakes. Could someone just point them out and offer a simpler way of doing it (possibly without using the Euclidean Algorithm)?

Thanks.

Michael

**Physics Forums - The Fusion of Science and Community**

# Gcd method

Have something to add?

- Similar discussions for: Gcd method

Loading...

**Physics Forums - The Fusion of Science and Community**