C/C ++ calculates the maximum common divisor and the minimum common multiple.

Source: Internet
Author: User

The method used below is called the division of the moving phase. The steps are as follows:

First use a small number to divide a large number and obtain the first remainder;
Use the first remainder to divide a small number and obtain the second remainder;
The second remainder is used to divide the first remainder, and the third remainder is obtained;


In this way, the remainder is removed from the last one until the remainder is 0. In this case, the last divisor is the maximum common divisor (if the last divisor is 1, the original two divisor is the mutual prime number ).

C language:

# Include <stdio. h>
Main ()
{
Int P, R, n, m, temp;
Printf ("enter two positive integers n, m"); // note when running this place. Cut them apart.
Scanf ("% d, % d", & N, & M );
P = N * m;
If (n <m)
{Temp = N;
N = m;
M = temp;
}

While (M! = 0)
{
R = n % m;
N = m;
M = R;
}
Printf ("their maximum common divisor: % d/N", N );
Printf ("their minimum public multiples are: % d/N", P/N );
}

 

In C ++:

# Include "iostream. H"
Void main ()
{
Int n, m, temp, R, J;
Cout <"Please input n = ";
Cin> N;
Cout <"Please input m = ";
Cin> m;
J = m * N;
If (n <m)
{
Temp = N;
N = m;
M = temp;
}
While (M! = 0)
{
R = n % m;
N = m;
M = R;
}
Cout <"their maximum common approx. is:" <n <Endl;
Cout <"their minimum public multiples are:" <j/n <Endl;

}

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.