When the if statement is deleted, the code runs without problems. What is the reason for that? This code gives the Greatest Common Divisor (GCD) of two numbers (m and n) the user should input.
#include <stdio.h>
int main() {
int m, n, r;
scanf("%d,%d", &m, &n);
if (m < n) {
r = m;
m = n;
n = r;
}
do {
r = m % n;
m = n;
n = r;
} while (r != 0);
printf("%d\n", m);
return 0;
}
It is not important whether
mis greater thann. Ifminitially is less thannthen in the first iteration of the do-while loopmwill be greater thanndue to the statementFor example let's assume that
mis equal to2andnis equal to10. Sorequal tom % nwill be equal to2and in fact these statementsswap
mandnin the first iteration of the loop.