[ create a new paste ] login | about

Link: http://codepad.org/9aNo5Qw4    [ raw code | output | fork ]

박정욱 - C++, pasted on Apr 20:
//객체지향 금123 2012.04.20
//4번		200911636박정욱
#include<iostream>
#include<iomanip>
using namespace std;

//최대공약수와 최소공배수를 구한다.
void GCD_LCM(int a, int b, int *gcd, int *lcm)
{	//유클리드의 호제법을 사용해서 GCD를 구한다.
	int z, x = a, y = b;
	while(1)
	{	z= x % y;
	if( z == 0)	{break;}
		x = y;
		y = z;
	}
	//결과를 저장한다.
	*gcd = y;
	*lcm = a*b /(*gcd);
}

int main()
{	//28과 35의 최대 공약수와 최소 공배수를 구한다.
	int gcd = 0;
	int lcm = 0;
	GCD_LCM(28,35, &gcd, &lcm);
	
	//결과를 출력한다.
	cout << "GCD = " << gcd << endl;
	cout << "LCM = " << lcm << endl;

	return 0;
}


Output:
1
2
GCD = 7
LCM = 140


Create a new paste based on this one


Comments: