C language calculates the greatest common divisor and maximum common multiple of two positive integers

Source: Internet
Author: User
Tags greatest common divisor

First give the source code, explained below.


#include <stdio.h>

void Main ()

{

int a,b,c,d; //define four variables

scanf ("%d,%d", &a,&b);

D=a*b; //Find out the product of two positive integers

while (b!=0)

{

C=a%b; //focus is here, a lot of people do not understand, and then look down

A=b;

B=c;

}

printf ("Greatest common divisor is%d, max Common multiple is%d\n", A, (d/a));

}

The method of least common multiple: a*b/greatest common divisor

So we find out greatest common divisor let D come apart.

The simplest is to use the Euclidean method, known in the West as Euclid's algorithm. Do not know Baidu a bit. Give the connection.

Http://baike.baidu.com/link?url=XfjUEUXTY7vO1H9NOQTWmRC8ACvW5miDkW16AZ5IrWhA3dQPiFYzPMcC-EI7Li0z0Qt-obgvLLxy6JSZffkena

Simply put: From the first start to calculate the a%b, the remainder C is obtained. The second time to assign the divisor B to a, let the original divisor to do dividend, the remainder of the divisor. If there is greatest common divisor, the last divisor is greatest common divisor. The greatest common divisor is calculated by dividing the product of two integers by greatest common divisor to obtain the least common multiple.

C language calculates the greatest common divisor and maximum common multiple of two positive integers

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.