Input two positive integers m and n, find their greatest common divisor and least common multiple. (required to be implemented with a while statement)
One, the greatest common divisor method
(1) tossing and dividing method
There are two integers a and b:
① a%b is the remainder c
② If c==0, then b is the greatest common divisor of the two numbers
③ If c! =0, then a=b, b=c, and then go back to execute ①.
For example, the process of finding the greatest common divisor of 27 and 15 is:
27÷15 remainder 12
15÷12 remainder 3
12÷3 remainder 0
Therefore, 3 is the greatest common divisor.
(2) Subtraction
There are two integers a and b:
① If a>b, then a=ab
② If a<b, then b=ba
③ If a==b, then a (or b) is the greatest common divisor of the two
numbers④ If a! =b, then go back and execute ①.
For example, the process of finding the greatest common divisor of 27 and 15 is:
27-15=12( 15>12 )
15-12=3( 12>3 )
12-3=9( 9>3 )
9-3=6( 6> 3)
6-3=3 (3==3)
Therefore, 3 is the greatest common divisor.
Second, find the least common multiple algorithm
Least common multiple = product of two integers ÷ greatest common divisor
code show as below:
#include <stdio.h>
int main()
{
int m,n,max,min,b,c;
printf("请输入两个整数:\n");
scanf("%d%d",&m,&n);
c=m%n;
b=m*n;
while(c!=0)
{
m=n;
n=c;
c=m%n;
}
max=n;
min=b/max;
printf("\n最大公约数为:%d\n最小公倍数为:%d\n",max,min);
return 0;
}
Output result: