Enter two positive integers m and n, and find the least common multiple LCM and greatest common divisor

Greatest common divisor

#include<stdio.h>
int main()
{
    
    
	int m,n,t,x;
	scanf("%d%d",&m,&n);
	if(m>n){
    
    
		t=m;
		m=n;
		n=t;
	}
	for(int i=1;i<=m;i++){
    
    
		if((m%i)==0&&(n%i==0)){
    
    
			x=i;
		}
	}
	printf("%d和%d的最大公约数是:%d",m,n,x);
	return 0;
}

Least Common Multiple LCM

Least common multiple of two numbers = product of two numbers/greatest common divisor

#include<stdio.h>
int main()
{
    
    
	int m,n,t,lcm,x;
	scanf("%d%d",&m,&n);
	if(m>n){
    
    
		t=m;
		m=n;
		n=t;
	}
	for(int i=1;i<=m;i++){
    
    
		if((m%i)==0&&(n%i==0)){
    
    
			x=i;
		}
	}
	lcm=m*n/x; 
	printf("%d和%d的最大公约数是:%d\n",m,n,x);
	printf("%d和%d的最小公倍数是:%d\n",m,n,lcm);
	return 0;
}

Guess you like

Origin blog.csdn.net/buxiangquaa/article/details/114989178