当前位置:网站首页>Find the greatest common divisor and the least common multiple (C language)
Find the greatest common divisor and the least common multiple (C language)
2022-07-07 10:34:00 【Pu Yu Mu Zhi】
List of articles
One 、 subject
Write two functions , Find the maximum common divisor and the minimum common multiple of two integers respectively , Call these two functions with the main function . Two integers are entered by the keyboard .
Two 、 Analysis and code
1. Ideas
- greatest common divisor : The largest of the common divisors of two or more integers . Calculate by rolling division .
- Minimum common multiple : Two or more integers share a multiple divided by 0 The smallest one outside . Because the product of two numbers is equal to the product of the greatest common divisor and the least common multiple of the two numbers , therefore , The least common multiple of two numbers is equal to the product of these two numbers divided by the greatest common divisor .
- Analysis methods : Define two functions respectively , And two variables a,b. The first function uses the rolling division method to find a,b Maximum common divisor of , hypothesis a>b, Make a/b Get the remainder r, then b As divisor ,r As divisor , Get the remainder … Until the remainder is 0. The second function uses the above formula to obtain the least common multiple of two numbers .
2. Code
The code is as follows :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Greatest_Common_Divisor(int a,int b); // Function declaration
int Least_Common_Multiple(int a,int b,int m); // Function declaration
int a,b,m,x;
scanf("%d,%d",&a,&b);
m=Greatest_Common_Divisor(a,b);
x=Least_Common_Multiple(a,b,m);
printf("%d and %d The greatest common divisor of :%d\n",a,b,m);
printf("%d and %d The minimum common multiple of is :%d\n",a,b,x);
return 0;
}
int Greatest_Common_Divisor(int a,int b) // Find the greatest common divisor
{
int temp,r;
if(b>a) // Let the larger of the two numbers be the dividend
{
temp=a;
a=b;
b=temp;
}
while((r=a%b)!=0)
{
a=b; // Divisor b Become a divisor a
b=r; // Make the remainder r Become a divisor b
}
return b;
}
int Least_Common_Multiple(int a,int b,int m) // Find the least common multiple
{
return (a*b/m);
}
3. Running results

边栏推荐
猜你喜欢

Deeply analyze the main contents of erc-4907 agreement and think about the significance of this agreement to NFT market liquidity!
![[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program](/img/03/41bb3870b9a6c2ee66099abac08eb3.png)
[STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program

leetcode-304:二维区域和检索 - 矩阵不可变

【推荐系统 01】Rechub

基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS

Several schemes of building hardware communication technology of Internet of things

【acwing】786. Number k

01 use function to approximate cosine function (15 points)

String formatting

Application of OpenGL gllightfv function and related knowledge of light source
随机推荐
使用U2-Net深层网络实现——证件照生成程序
MySQL insert data create trigger fill UUID field value
Vs code specifies the extension installation location
leetcode-560:和为 K 的子数组
Several schemes of building hardware communication technology of Internet of things
Prototype object in ES6
Installation and configuration of slurm resource management and job scheduling system
CAS mechanism
JS实现链式调用
【acwing】786. Number k
浅谈日志中的返回格式封装格式处理,异常处理
对word2vec的一些浅层理解
Deeply analyze the main contents of erc-4907 agreement and think about the significance of this agreement to NFT market liquidity!
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
How embedded engineers improve work efficiency
0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
嵌入式工程师如何提高工作效率
leetcode-303:区域和检索 - 数组不可变
BigDecimal value comparison
Leetcode-304: two dimensional area and retrieval - matrix immutable