# Program to find the LCM of two prime numbers

• Last Updated : 27 Jun, 2021

Given two prime numbers N and M, the task is to find the Least Common Multiple(LCM) of the two given prime numbers.
Examples:

Input: N = 3, M = 7
Output: 21
Explanation:
The least numbers greater than equals to 3 and 7 which is a multiple of 3 and 7 is 21.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Input: N = 5, M = 5
Output:
Explanation:
The least numbers greater than equals to 5 and 5 which is a multiple of 5 and 5 is 5.

Approach: As we know that product of two numbers equals to the product of their Greatest Common Divisor(GCD) and Least Common Multiple(LCM). So, the LCM of the two given prime numbers can be given by: .
Since the GCD two different prime numbers are 1, Therefore , and if the two given numbers are same then the LCM is the number itself.

Below is the implementation of the above approach:

## C++

 // C++ Program to find LCM of two// prime numbers#include using namespace std; // Function to return the LCM of two// prime numbersint findLCMPrime(int a, int b){    // If the two numbers are equal    // then return any one of a and b    if (a == b) {        return a;    }     // Else return product of numbers    return a * b;} // Driver codeint main(){    // Given two numbers    int a = 3, b = 5;     // Function Call    cout << findLCMPrime(a, b);    return 0;}

## Java

 // Java Program to find LCM of two// prime numbersclass GFG{     // Function to return the LCM of two// prime numbersstatic int findLCMPrime(int a, int b){    // If the two numbers are equal    // then return any one of a and b    if (a == b)    {        return a;    }     // Else return product of numbers    return a * b;} // Driver codepublic static void main (String[] args){    // Given two numbers    int a = 3, b = 5;     // Function Call    System.out.println(findLCMPrime(a, b));}} // This code is contributed by AnkitRai01

## Python3

 # Python3 program to find LCM of two# prime numbers # Function to return the LCM of two# prime numbersdef findLCMPrime(a, b):     # If the two numbers are equal    # then return any one of a and b    if (a == b):        return a;     # Else return product of the numbers    return a * b; # Driver codeif __name__ == "__main__":     # Given two numbers    a = 3; b = 5;     # Function Call    print(findLCMPrime(a, b)); # This code is contributed by AnkitRai01

## C#

 // C# program to find LCM of two prime numbersusing System; class GFG{     // Function to return the LCM of two// prime numbersstatic int findLCMPrime(int a, int b){         // If the two numbers are equal    // then return any one of a and b    if (a == b)    {        return a;    }         // Else return product of numbers    return a * b;}     // Driver codepublic static void Main (string[] args){         // Given two numbers    int a = 3, b = 5;         // Function Call    Console.WriteLine(findLCMPrime(a, b));}} // This code is contributed by AnkitRai01

## Javascript

 
Output
15

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up