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

边栏推荐
- 2022年上半年5月网络工程师试题及答案
- 【推荐系统 01】Rechub
- JS实现链式调用
- Summary of router development knowledge
- leetcode-304:二维区域和检索 - 矩阵不可变
- String formatting
- Multithreaded asynchronous orchestration
- I'd rather say simple problems a hundred times than do complex problems once
- 中级软件评测师考什么
- The width of table is 4PX larger than that of tbody
猜你喜欢

软考信息处理技术员有哪些备考资料与方法?

IIC基本知识

Socket communication principle and Practice

【acwing】786. Number k

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

Openinstall and Hupu have reached a cooperation to mine the data value of sports culture industry

Deeply analyze the main contents of erc-4907 agreement and think about the significance of this agreement to NFT market liquidity!

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

深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!

Basic introduction of yarn and job submission process
随机推荐
Study summary of postgraduate entrance examination in September
Easyexcel read write simple to use
Some properties of leetcode139 Yang Hui triangle
BigDecimal数值比较
软考一般什么时候出成绩呢?在线蹬?
leetcode-560:和为 K 的子数组
无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。
使用Tansformer分割三维腹部多器官--UNETR实战
Serial communication relay Modbus communication host computer debugging software tool project development case
Socket communication principle and Practice
CAS机制
软考中级,软件设计师考试那些内容,考试大纲什么的?
Study summary of postgraduate entrance examination in November
Remote meter reading, switching on and off operation command
【acwing】789. Range of numbers (binary basis)
Kotlin realizes wechat interface switching (fragment exercise)
Schnuka: machine vision positioning technology machine vision positioning principle
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
施努卡:机器人视觉抓取工作原理 机器视觉抓取
Study summary of postgraduate entrance examination in October