# C++How to calculate the greatest common divisor of two numbers in C++.

#### Veronica

##### Coder
We can traverse over all the numbers from min(A, B) to 1 and check if the current number divides both A and B or not. If it does, then it will be the GCD of A and B.
Code:
``````int GCD(int A, int B) {
int m = min(A, B), gcd;
for(int i = m; i > 0; --i)
if(A % i == 0 && B % i == 0) {
gcd = i;
return gcd;
}
}``````

I am a beginner in c++ programming and I am stuck on the problem of finding the GCD (greatest common divisor) of two non-negative integers, a and b. I have taken a code reference from a post related to this problem. Could you please let me know if the code is correct?

#### cbreemer

##### King Coder
Why would that code not be correct ? Are you running into a problem with it ? Or don't you trust that site ?
And what do you mean you are "stuck" ? You'll have to show what you've done, and explain how it failed or why you cannot proceed. Being a beginner does not excuse you from giving it your best shot.

#### BlackCube

##### Coder
Poor style.

Remember structured programming? The idea of code blocks. Start at the top, finish at the bottom.

Jumping out of a function from inside a for loop, madness. ;-)

#### cbreemer

##### King Coder
Jumping out of a function from inside a for loop, madness. ;-)
Question of taste, methinks. The return will end the loop as surely as a break would. If you find you're done you may as well quit the function there and then.
But if there's any technical reason not to do this (rather than an ethical reason) I would like to hear.

#### blueriss

##### New Coder
The code looks correct and should be able to find the GCD of two non-negative integers. However, it is always a good idea to test the code to make sure it is producing the correct results.

#### blueriss

##### New Coder
This industry is highly competitive. As such, it is important for software development companies to stay updated with the latest trends and technologies in order to remain competitive. A company should invest in training and development of its staff, while also maintaining contacts with the changing technological world. Additionally, they need to have reliable processes in place in order to ensure their projects are delivered on time and on budget. Having a strong portfolio, keeping up with the competition, and continuously doing market research are all essential for any successful MLSDev.