C language greatest common divisor and least common multiple - array version

Title Description

Given two positive integers x and y, find the greatest common divisor of two integers n and least common multiple.

Requirements (can not write a separate function gcd and lcm, does not meet the requirements will be counted wrong) use the following interfaces:

void get_gcdANDlcm(int x, int y, int arr[]);

arr [0] should be placed greatest common divisor, arr [1] should put the least common multiple.

Entry

Comprising a plurality of sets of input data.

Each set of data separated by a space two positive integers x and y components. x and y are not more than 4,000.

Export

For each input, output and least common multiple of the greatest common divisor thereof, see the examples.

Sample input Copy

3 6
4 8

Sample output Copy

GCD=3,LCM=6
GCD=4,LCM=8

Code

#include <stdio.h>
#include<math.h> 
void get_gcdANDlcm(int x, int y, int  arr[])
{
	int t;
	arr[1]=x*y;
	if(x>y)
	{
		t=y;y=x;x=t;
	}
	while(x)
	{
		if(y%x==0)
		{
			arr[0]=x;
			break;
		}
		t=y;
		y=x;
		x=t%x;
	}
	arr[1]=arr[1]/arr[0];
	printf("GCD=%d,LCM=%d\n",arr[0],arr[1]);
}
int main()
{	
	int x,g,y,a[2]={'\0'};
    
    while(scanf("%d %d",&x,&y)!=EOF)
    {
    	get_gcdANDlcm(x,y,a);
	}
    return 0;
}
Published 47 original articles · won praise 29 · views 1485

Guess you like

Origin blog.csdn.net/Qianzshuo/article/details/103758714