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

边栏推荐
- 1323:【例6.5】活动选择
- Vs code specifies the extension installation location
- table宽度比tbody宽度大4px
- 施努卡:机器视觉定位技术 机器视觉定位原理
- Kotlin realizes wechat interface switching (fragment exercise)
- leetcode-303:区域和检索 - 数组不可变
- [sword finger offer] 42 Stack push in and pop-up sequence
- IDA中常见快捷键
- 如何顺利通过下半年的高级系统架构设计师?
- Sword finger offer 38 Arrangement of strings [no description written]
猜你喜欢

XML configuration file parsing and modeling

Cluster task scheduling system lsf/sge/slurm/pbs based on HPC scenario

ArrayList thread insecurity and Solutions

Leetcode-304: two dimensional area and retrieval - matrix immutable

对word2vec的一些浅层理解

多线程-异步编排

openinstall与虎扑达成合作,挖掘体育文化产业数据价值

Summary of router development knowledge

PHP \ newline cannot be output

无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。
随机推荐
使用 load_decathlon_datalist (MONAI)快速加载JSON数据
Socket communication principle and Practice
深入理解Apache Hudi异步索引机制
Applet jump to H5, configure business domain name experience tutorial
Leetcode-304: two dimensional area and retrieval - matrix immutable
PHP \ newline cannot be output
关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
Common shortcut keys in IDA
宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
1321:【例6.3】删数问题(Noip1994)
Multisim -- software related skills
Openinstall and Hupu have reached a cooperation to mine the data value of sports culture industry
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
Vs code specifies the extension installation location
P1223 queuing for water /1319: [example 6.1] queuing for water
ThreadLocal is not enough
Application of OpenGL gllightfv function and related knowledge of light source
JS实现链式调用
555 circuit details