当前位置:网站首页>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

边栏推荐
- 求最大公约数与最小公倍数(C语言)
- Socket communication principle and Practice
- I'd rather say simple problems a hundred times than do complex problems once
- Remote meter reading, switching on and off operation command
- Using U2 net deep network to realize -- certificate photo generation program
- Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
- 优雅的 Controller 层代码
- 如何顺利通过下半年的高级系统架构设计师?
- TypeScript 接口继承
- Talking about the return format in the log, encapsulation format handling, exception handling
猜你喜欢
随机推荐
IIC基本知识
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
gym安装踩坑记录
Socket通信原理和实践
1324:【例6.6】整数区间
Using U2 net deep network to realize -- certificate photo generation program
Use the fetch statement to obtain the repetition of the last row of cursor data
2022.7.6DAY598
[email protected]能帮助我们快速拿到日志对象
IDA中常见快捷键
【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE
Slurm资源管理与作业调度系统安装配置
[牛客网刷题 Day6] JZ27 二叉树的镜像
Jump to the mobile terminal page or PC terminal page according to the device information
ADB utility commands (network package, log, tuning related)
[sword finger offer] 42 Stack push in and pop-up sequence
String formatting
[detailed explanation of Huawei machine test] tall and short people queue up
BigDecimal数值比较
软考中级,软件设计师考试那些内容,考试大纲什么的?








![[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树](/img/ba/b2dfbf121798757c7b9fba4811221b.png)