当前位置:网站首页>函数:求两个正数的最大公约数和最小公倍
函数:求两个正数的最大公约数和最小公倍
2022-07-06 09:24:00 【|光|】
要求
写两个函数,分别求两个正数的最大公约数和最小公倍数,用主函数调用这两个函数并输出结果。两个正数由键盘输入。
代码
/* * 该函数用来计算两个数的最大公约数 */
int greatest_common_divisor(int m,int n)
{
int gcd=1,i;
for(i=0;i<10;i++)
{
if(m%2==0 && n%2==0)
{
gcd = gcd*2;
m=m/2;
n=n/2;
}
else if(m%3==0 && n%3==0)
{
gcd = gcd*3;
m=m/3;
n=n/3;
}
else if(m%5==0 && n%5==0)
{
gcd = gcd*5;
m=m/5;
n=n/5;
}
else if(m%7==0 && n%7==0)
{
gcd = gcd*7;
m=m/7;
n=n/7;
}
else
i=10;
}
return gcd;
}
/* * 该函数用来计算两个数的最小公倍数 */
int least_common_multiple(int m,int n)
{
int lcm=0;
lcm = m*n/greatest_common_divisor(m,n);
return lcm;
}
测试
测试输入
12 4
输出
两个数的最大公约数为:4,最小公倍数为:12
边栏推荐
- XSS (cross site scripting attack) for security interview
- 使用 flask_whooshalchemyplus jieba实现flask的全局搜索
- Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
- 《统计学》第八版贾俊平第一章课后习题及答案总结
- The most popular colloquial system explains the base of numbers
- 网络基础详解
- 内网渗透之内网信息收集(五)
- Only 40% of the articles are original? Here comes the modification method
- sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
- The difference between layer 3 switch and router
猜你喜欢

链队实现(C语言)

网络层—简单的arp断网

xray与burp联动 挖掘

Constants, variables, and operators of SystemVerilog usage

JVM memory model concept

JDBC事务、批处理以及连接池(超详细)

Proceedingjoinpoint API use

数据库多表链接的查询方式

Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary

Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
随机推荐
《统计学》第八版贾俊平第五章概率与概率分布
The United States has repeatedly revealed that the yield of interest rate hiked treasury bonds continued to rise
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Realize applet payment function with applet cloud development (including source code)
Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
Based on authorized access, cross host, and permission allocation under sqlserver
Sword finger offer 23 - print binary tree from top to bottom
SQL注入
2022华中杯数学建模思路
使用 flask_whooshalchemyplus jieba实现flask的全局搜索
Detailed explanation of three ways of HTTP caching
AQS details
JDBC事务、批处理以及连接池(超详细)
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
. Net6: develop modern 3D industrial software based on WPF (2)
安全面试之XSS(跨站脚本攻击)
【指针】查找最大的字符串
Markdown font color editing teaching
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
Proceedingjoinpoint API use