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.

Input:N = 5, M = 5

Output:5

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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the LCM of two ` `// prime numbers ` `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 code ` `int` `main() ` `{ ` ` ` `// Given two numbers ` ` ` `int` `a = 3, b = 5; ` ` ` ` ` `// Function Call ` ` ` `cout << findLCMPrime(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find LCM of two ` `// prime numbers ` `class` `GFG{ ` ` ` `// Function to return the LCM of two ` `// prime numbers ` `static` `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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find LCM of two ` `# prime numbers ` ` ` `# Function to return the LCM of two ` `# prime numbers ` `def` `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 code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Given two numbers ` ` ` `a ` `=` `3` `; b ` `=` `5` `; ` ` ` ` ` `# Function Call ` ` ` `print` `(findLCMPrime(a, b)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find LCM of two prime numbers ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return the LCM of two ` `// prime numbers ` `static` `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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

**Output:**

15

**Time Complexity:** *O(1)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Program to find LCM of two numbers
- Program to find LCM of two Fibonnaci Numbers
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Find two numbers with the given LCM and minimum possible difference
- Find two numbers with given sum and maximum possible LCM
- Find two distinct numbers such that their LCM lies in given range
- Program to find LCM of 2 numbers without using GCD
- Check if a prime number can be expressed as sum of two Prime Numbers
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Finding LCM of more than two (or array) numbers without using GCD
- LCM of two large numbers
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Prime factors of LCM of array elements
- Check if LCM of array elements is divisible by a prime number or not
- Find LCM of rational numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.