public class GCDLCM {
public static void main(String[] args) {
int num1 = 12;
int num2 = 15;
int gcd = findGCD(num1, num2);
int lcm = findLCM(num1, num2, gcd);
System.out.println("GCD of " + num1 + " and " + num2 + " is: " + gcd);
System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);
}
public static int findGCD(int a, int b) {
if (b == 0) {
return a;
}
return findGCD(b, a % b);
}
public static int findLCM(int a, int b, int gcd) {
return (a * b) / gcd;
}
}
In this program, we have two methods: findGCD and findLCM. The findGCD method uses the Euclidean algorithm to find the GCD of two numbers, while the findLCM method calculates the LCM using the formula (a * b) / gcd. The main method initializes two numbers, num1 and num2, and then calls the findGCD and findLCM methods to calculate their GCD and LCM, respectively. Finally, the results are printed to the screen.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In this program, we have two methods: findGCD and findLCM. The findGCD method uses the Euclidean algorithm to find the GCD of two numbers, while the findLCM method calculates the LCM using the formula (a * b) / gcd. The main method initializes two numbers, num1 and num2, and then calls the findGCD and findLCM methods to calculate their GCD and LCM, respectively. Finally, the results are printed to the screen.