C语言程序查找最大公约数 (HCF) 和最小公倍数 (LCM)
首先,让我们学习如何查找最大公约数 (HCF)。
最大公约数 (HCF)
能同时整除两个或多个数的最大整数称为 HCF 或最大公约数。它也称为最大公因数 (GCM) 和最大公约数 (GCD)。
例如,
12 和 16 的 HCF 是多少?
Factors of 12 = 1, 2, 3, 4, 6,12. Factors of 16=1,2,4,8,16
12 和 16 的最大公约数 (H.C.F) = 4。
最小公倍数 (LCM)
对于两个整数 x 和 y,用 LCM(x,y) 表示,它是能同时被 x 和 y 整除的最小正整数。
例如,
LCM(2,3) = 6 and LCM(6,10) = 30.
示例
#include <stdio.h>
int main() {
int num1, num2, x, y, temp, gcd, lcm;
printf("Enter two integers
");
scanf("%d%d", &x, &y);
num1 = x;
num2 = y;
while (num2 != 0) {
temp = num2;
num2 = num1 % num2;
num1 = temp;
}
gcd = num1;
lcm = (x*y)/gcd;
printf("GCD of %d and %d = %d
", x, y, gcd);
printf("LCM of %d and %d = %d
", x, y, lcm);
return 0;
}输出
执行后,您将收到以下输出:
Run 1: Enter two integers 6 12 GCD of 6 and 12 = 6 LCM of 6 and 12 = 12 Run 2: Enter two integers 24 36 GCD of 24 and 36 = 12 LCM of 24 and 36 = 72
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP