---
title: "Write a java program to find the GCD and LCM of two given numbers."  
description: "Write a java program to find the GCD and LCM of two given numbers."  
author: "Revati S Misra"  
published: 2023-04-19  
updated: 2023-04-24  
canonical: https://www.mindstick.com/forum/157931/write-a-java-program-to-find-the-gcd-and-lcm-of-two-given-numbers  
category: "java"  
tags: ["java", "programs"]  
reading_time: 2 minutes  

---

# Write a java program to find the GCD and LCM of two given numbers.

Write a [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to find the GCD and LCM of two given numbers.

## Replies

### Reply by Aryan Kumar

```java
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.


---

Original Source: https://www.mindstick.com/forum/157931/write-a-java-program-to-find-the-gcd-and-lcm-of-two-given-numbers

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
