# Explanation for GCD while loop

Page 1 of 1

## 3 Replies - 4423 Views - Last Post: 28 February 2010 - 05:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=157605&amp;s=2318a95ecd241fb9b7e789f8ed902947&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 luron31

Reputation: 0
• Posts: 53
• Joined: 19-July 09

# Explanation for GCD while loop

Posted 22 February 2010 - 11:14 AM

Can someone explain this code line by line?

the int B is supposed to b
```
public static int gcd(int a, int B)/>
{
int gcd;
while (b != 0)
{
int temp = a % b;
a = b;
b = temp;
}
gcd =  Math.abs(a);
return gcd;
}

```

This post has been edited by luron31: 22 February 2010 - 11:19 AM

Is This A Good Question/Topic? 0

## Replies To: Explanation for GCD while loop

### #2 macosxnerd101

• Self-Trained Economist

Reputation: 9156
• Posts: 33,975
• Joined: 27-December 08

## Re: Explanation for GCD while loop

Posted 22 February 2010 - 01:36 PM

Yes- B should be lowercased. Also, this sounds like a homework assignment. Is there a specific part you are unsure about? If so, can you talk us through what you do understand, what you think each part does, and what you are unsure about?

### #3 luron31

Reputation: 0
• Posts: 53
• Joined: 19-July 09

## Re: Explanation for GCD while loop

Posted 28 February 2010 - 01:34 PM

macosxnerd101, on 22 February 2010 - 12:36 PM, said:

Yes- B should be lowercased. Also, this sounds like a homework assignment. Is there a specific part you are unsure about? If so, can you talk us through what you do understand, what you think each part does, and what you are unsure about?

Nevermind. Thanks

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8066
• Posts: 31,310
• Joined: 06-March 08

## Re: Explanation for GCD while loop

Posted 28 February 2010 - 05:35 PM

```public static int gcd(int a, int B)/>
{
int gcd;

```

even if legal because the compiler is smart NEVER NEVER NEVER a good idea to have a method() and a variable with the same name. Here: gcd
It is just make things more confusing