求两个正整数的最小公倍数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45663523/article/details/102749424

1、求两个正整数的最小公倍数
2、程序:
第一种方法:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int temp = 0;
int main(){
	printf("Please input two numbers:");
	int A, B;
	int i ,q;
	scanf("%d %d", &A, &B);
	q = A*B;
	if (A > B){
		temp = A;
	}
	else{
		temp = B;
	}
	for (i = temp; i < q + 1; ++i){
		if ((i%A == 0) && (i%B == 0)){
			printf("%d\n", i);
			break;
		}
	}

 system("pause");
 return 0;
}

方法二:

#include<stdio.h>
int main()
{
int A, B;
scanf("%d%d", &A, &B);
int i = 1;
while (((A * i) % B) != 0)
{
i++;
}
printf("%d\n", A*i);
return 0;
}

A*i的结果一定迟早会成为与B有关系的数,此时的值为A与B的最小公倍数
3、结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45663523/article/details/102749424
今日推荐