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

边栏推荐
- Openinstall and Hupu have reached a cooperation to mine the data value of sports culture industry
- [second on] [jeecgboot] modify paging parameters
- Prototype and prototype chain
- Cluster task scheduling system lsf/sge/slurm/pbs based on HPC scenario
- gym安装踩坑记录
- 想考中级软考,一般需要多少复习时间?
- The mobile terminal automatically adjusts the page content and font size by setting rem
- 多线程-异步编排
- [dai6] mirror image of JZ27 binary tree
- PHP \ newline cannot be output
猜你喜欢

小程序跳转H5,配置业务域名经验教程

leetcode-560:和为 K 的子数组

Application of OpenGL gllightfv function and related knowledge of light source

Trajectory planning for multi robot systems: methods and Applications Overview reading notes

Some superficial understanding of word2vec

中级软件评测师考什么

【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法

Some properties of leetcode139 Yang Hui triangle

如何顺利通过下半年的高级系统架构设计师?

Socket通信原理和实践
随机推荐
施努卡:机器视觉定位技术 机器视觉定位原理
Leetcode-304: two dimensional area and retrieval - matrix immutable
BUUCTF---Reverse---reverse1
P1031 [noip2002 improvement group] average Solitaire
施努卡:机器人视觉抓取工作原理 机器视觉抓取
CAS mechanism
About hzero resource error (groovy.lang.missingpropertyexception: no such property: weight for class)
Easyexcel read write simple to use
小程序跳转H5,配置业务域名经验教程
Summary of router development knowledge
使用U2-Net深层网络实现——证件照生成程序
MONAI版本更新到 0.9 啦,看看有什么新功能
对word2vec的一些浅层理解
Serial communication relay Modbus communication host computer debugging software tool project development case
[daiy5] jz77 print binary tree in zigzag order
ArrayList thread insecurity and Solutions
1323: [example 6.5] activity selection
软考信息处理技术员有哪些备考资料与方法?
Kotlin实现微信界面切换(Fragment练习)
Prototype and prototype chain