RosettaCodeData/Task/Least-common-multiple/C++/least-common-multiple-2.cpp

25 lines
504 B
C++
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
#include <cstdlib>
#include <iostream>
#include <tuple>
int gcd(int a, int b) {
a = abs(a);
b = abs(b);
while (b != 0) {
2018-06-22 20:57:24 +00:00
std::tie(a, b) = std::make_tuple(b, a % b);
2015-11-18 06:14:39 +00:00
}
return a;
}
int lcm(int a, int b) {
int c = gcd(a, b);
return c == 0 ? 0 : a / c * b;
}
int main() {
2018-06-22 20:57:24 +00:00
std::cout << "The least common multiple of 12 and 18 is " << lcm(12, 18) << ",\n"
<< "and their greatest common divisor is " << gcd(12, 18) << "!"
<< std::endl;
2015-11-18 06:14:39 +00:00
return 0;
}