Returns two positive integers, m, n, and the smallest common number.

Source: Internet
Author: User

The following code calculates the maximum common divisor and least common multiple of two positive integers, m and n:
# Include <stdio. h>
Void main ()
{
Int m, n, gcd, lcm;/gcd is the maximum common approx. lcm is the least common multiple/
Int max, min, swap;/max is m, n is smaller than min, n is smaller/
Printf ("input two positive integers m and n ");
Scanf ("% d", & m, & n );
Max = m> n? M: n;/returns m. n is greater than limit to max/
Min = m <n? M: n;/return m. The smaller value in n is min/
Swap = min;/obtain the value when the value of swap is smaller than that of swap/
If (max % min = 0)
{
Gcd = min;
Printf ("maximum common approx. gcd = % d", gcd );
}
While (swap! = 0)
{
Swap = swap-1;/find a smaller value/
If (max % swap = 0 & min % swap = 0)
{
Gcd = swap;
Printf ("maximum common approx. gcd = % d", gcd );
Break;/find the stop loop/
}
}
Lcm = m * n/gcd;
Printf ("minimum public factor lcm = % d", lcm );
}

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.