GCD and LCM Method-1¶ C++ find_GCD-LCM.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26#include <bits/stdc++.h> using namespace std; int GCD(int a, int b){ int gcd = 0; for (int i = 1; i <= min(a, b); i++) { if (a % i == 0 and b % i == 0) { gcd = i; } } return gcd; } int LCM(int a, int b){ int gcd = GCD(a, b); int lcm = (a*b)/gcd; return lcm; } int main() { int a, b; cin >> a >> b; cout << "GCD: " << GCD(a, b) << endl; cout << "LCM: " << LCM(a, b) << endl; return 0; } Input: 5 10 Output: GCD: 5 LCM: 10 Method-2¶ C++ find_GCD-LCM.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23#include <bits/stdc++.h> using namespace std; int GCD(int a, int b){ if (b == 0) { return a; } return GCD(b, a % b); } int LCM(int a, int b){ int gcd = GCD(a, b); int lcm = (a*b)/gcd; return lcm; } int main() { int a, b; cin >> a >> b; cout << "GCD: " << GCD(a, b) << endl; cout << "LCM: " << LCM(a, b) << endl; return 0; } Input: 5 10 Output: GCD: 5 LCM: 10