当前位置:网站首页>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
边栏推荐
- 小程序跳转H5,配置业务域名经验教程
- 【华为机试真题详解】高矮个子排队
- 打算参加安全方面工作,信息安全工程师怎么样,软考考试需要怎么准备?
- Gym installation pit records
- 软考信息处理技术员有哪些备考资料与方法?
- 软考中级电子商务师含金量高嘛?
- Trajectory planning for multi robot systems: methods and Applications Overview reading notes
- 软考中级,软件设计师考试那些内容,考试大纲什么的?
- Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
- Study summary of postgraduate entrance examination in July
猜你喜欢
JMeter loop controller and CSV data file settings are used together
优雅的 Controller 层代码
Smart city construction based on GIS 3D visualization technology
Talking about the return format in the log, encapsulation format handling, exception handling
南航 PA3.1
【实战】霸榜各大医学分割挑战赛的Transformer架构--nnFormer
555电路详解
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
Vs code specifies the extension installation location
Review of the losers in the postgraduate entrance examination
随机推荐
【推荐系统 01】Rechub
JMeter about setting thread group and time
Study summary of postgraduate entrance examination in November
Slurm资源管理与作业调度系统安装配置
BigDecimal value comparison
JMeter installation
ThreadLocal is not enough
Smart city construction based on GIS 3D visualization technology
深入理解Apache Hudi异步索引机制
Prototype object in ES6
High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
[email protected] can help us get the log object quickly
Multithreaded asynchronous orchestration
Schnuka: machine vision positioning technology machine vision positioning principle
软考信息处理技术员有哪些备考资料与方法?
基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
中级软件评测师考什么
[daiy5] jz77 print binary tree in zigzag order
1323: [example 6.5] activity selection
【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE