Returns the maximum common divisor of two integers.
Chapter 2 of C and pointers: 7th programming questions:
The maximum number of M and N (both M and N are greater than 0) values can be calculated as follows:
Compile a function named gcd. It accepts two Integer Parameters and returns the maximum public approx. If none of the two parameters is greater than zero, the function returns zero.
1/* 2 ** calculate the maximum common approx. Of two integers 3 */4 5 # include <stdio. h> 6 7 int gcd (int M, int N); 8 9 int 10 main () 11 {12 int m, n; 13 scanf ("% d ", & m, & n); 14 printf ("% d", gcd (m, n); 15 return 0; 16} 17 18/* 19 ** calculate the maximum approximate number of two integers 20 ** if either of the two parameters is not greater than 0, the function returns 021 */22 int 23 gcd (int M, int N) 24 {25 int R, t; 26 27/* 28 ** if M, N if any number is not greater than 0, the function returns 029 */30 if (M <= 0 | N <= 0) 31 return 0; 32 33/* 34 ** ensure M> = N35 ** this part is redundant thanks to @ garbageMan's correction 36 if (M <N) 37 {38 t = M; 39 M = N; 40 N = t; 41} 42 */43 44/* 45 ** calculate the maximum approximate number 46 */47 while (R = M % N)> 0) 48 {49 M = N; 50 N = R; 51} 52 53 return N; 54}