Returns the maximum common divisor of two integers.

Source: Internet
Author: User

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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.