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.
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?
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?