当前位置:网站首页>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
边栏推荐
- Mendeley--免费的文献管理工具,给论文自动插入参考文献
- When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
- 1323: [example 6.5] activity selection
- I'd rather say simple problems a hundred times than do complex problems once
- 字符串格式化
- Study summary of postgraduate entrance examination in October
- table宽度比tbody宽度大4px
- 打算参加安全方面工作,信息安全工程师怎么样,软考考试需要怎么准备?
- Elegant controller layer code
- 深入理解Apache Hudi异步索引机制
猜你喜欢
随机推荐
I'd rather say simple problems a hundred times than do complex problems once
ArrayList thread insecurity and Solutions
Several schemes of building hardware communication technology of Internet of things
多线程-异步编排
Serial communication relay Modbus communication host computer debugging software tool project development case
Schnuka: machine vision positioning technology machine vision positioning principle
[email protected]能帮助我们快速拿到日志对象
Easyexcel read write simple to use
Multithreaded asynchronous orchestration
软考中级,软件设计师考试那些内容,考试大纲什么的?
Kotlin realizes wechat interface switching (fragment exercise)
555电路详解
[sword finger offer] 42 Stack push in and pop-up sequence
Remote meter reading, switching on and off operation command
Study summary of postgraduate entrance examination in September
Leetcode-560: subarray with sum K
IIC基本知识
高级软考(网络规划设计师)该如何备考?
Applet jump to H5, configure business domain name experience tutorial
JMeter installation