Program to find LCM of two numbers
Last Updated :
14 Feb, 2025
LCM of two numbers is the smallest number which can be divided by both numbers.
Input : a = 12, b = 18
Output : 36
36 is the smallest number divisible by both 12 and 18
Input : a = 5, b = 11
Output : 55
55 is the smallest number divisible by both 5 and 11
[Naive Approach] Using Conditional Loop
This approach to calculating the Least Common Multiple (LCM) involves starting from the greater of the two numbers and checking if it's divisible by the smaller number. It iterates through multiples of the larger number, incrementing by the larger number itself in each step. The first multiple that is divisible by the smaller number is the LCM. This method is simple and intuitive, but it can be inefficient, especially for large numbers, as it checks multiple values until a match is found.
C++
// C++ program to find LCM of 2 numbers
#include <bits/stdc++.h>
using namespace std;
// Function to return LCM of two numbers
int LCM(int a, int b)
{
int greater = max(a, b);
int smallest = min(a, b);
for (int i = greater; ; i += greater) {
if (i % smallest == 0)
return i;
}
}
// Driver program to test above function
int main()
{
int a = 10, b = 5;
cout << "LCM of " << a << " and "
<< b << " is " << LCM(a, b);
return 0;
}
C
#include <stdio.h>
// Function to return LCM of two numbers
int LCM(int a, int b)
{
int greater = (a > b) ? a : b;
int smallest = (a < b) ? a : b;
for (int i = greater; ; i += greater) {
if (i % smallest == 0)
return i;
}
}
// Driver program to test above function
int main()
{
int a = 10, b = 5;
printf("LCM of %d and %d is %d\n", a, b, LCM(a, b));
return 0;
}
Java
// Java program to find LCM of 2 numbers
import java.util.Scanner;
public class LCM {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = 10;
int b = 5;
int lcm = findLCM(a, b);
System.out.println("LCM of " + a + " and " + b
+ " is " + lcm);
sc.close();
}
// Function to return LCM of two numbers
public static int findLCM(int a, int b)
{
int greater = Math.max(a, b);
int smallest = Math.min(a, b);
for (int i = greater;; i += greater) {
if (i % smallest == 0)
return i;
}
}
}
Python
# Python program to find LCM of two numbers
# Function to return LCM of two numbers
def LCM(a, b):
greater = max(a, b)
smallest = min(a, b)
for i in range(greater, a*b+1, greater):
if i % smallest == 0:
return i
# Driver program to test above function
if __name__ == '__main__':
a = 10
b = 5
print("LCM of", a, "and", b, "is", LCM(a, b))
C#
// C# program to find LCM of 2 numbers
using System;
class LCMProgram
{
// Function to return LCM of two numbers
static int LCM(int a, int b)
{
int greater = Math.Max(a, b);
int smallest = Math.Min(a, b);
for (int i = greater;; i += greater) {
if (i % smallest == 0)
return i;
}
}
// Driver program to test above function
static void Main()
{
int a = 10, b = 5;
Console.WriteLine("LCM of " + a + " and " + b
+ " is " + LCM(a, b));
}
}
JavaScript
// Javascript program to find LCM of two numbers
// Function to return LCM of two numbers
function LCM(a, b){
let greater = Math.max(a, b);
let smallest = Math.min(a, b);
for(let i = greater; i <= a*b; i+=greater){
if(i % smallest == 0){
return i;
}
}
}
// Driver program to test above function
let a = 10;
let b = 5;
console.log("LCM of", a, "and", b, "is", LCM(a, b));
OutputLCM of 10 and 5 is 10
Time Complexity: O(min(a,b))
Auxiliary Space: O(1)
[Expected Approach] Using GCD LCM Formula
An efficient solution is based on the below formula for LCM of two numbers 'a' and 'b'.

a x b = LCM(a, b) * GCD (a, b)
LCM(a, b) = (a x b) / GCD(a, b)
We have discussed function to find GCD of two numbers. Using GCD, we can find LCM.
Below is the implementation of the above idea:
C++
// C++ program to find LCM of two numbers
#include <iostream>
using namespace std;
// Recursive function to return gcd of a and b
long long gcd(long long int a, long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to return LCM of two numbers
long long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// Driver program to test above function
int main()
{
int a = 15, b = 20;
cout <<"LCM of " << a << " and "
<< b << " is " << lcm(a, b);
return 0;
}
C
// C program to find LCM of two numbers
#include <stdio.h>
// Recursive function to return gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Function to return LCM of two numbers
int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// Driver program to test above function
int main()
{
int a = 15, b = 20;
printf("LCM of %d and %d is %d ", a, b, lcm(a, b));
return 0;
}
Java
// Java program to find LCM of two numbers.
import java.io.*;
public class Test
{
// Recursive method to return gcd of a and b
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// method to return LCM of two numbers
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// Driver method
public static void main(String[] args)
{
int a = 15, b = 20;
System.out.println("LCM of " + a +
" and " + b +
" is " + lcm(a, b));
}
}
Python
# Python program to find LCM of two numbers
# Recursive function to return gcd of a and b
def gcd(a,b):
if a == 0:
return b
return gcd(b % a, a)
# Function to return LCM of two numbers
def lcm(a,b):
return (a // gcd(a,b))* b
# Driver program to test above function
a = 15
b = 20
print('LCM of', a, 'and', b, 'is', lcm(a, b))
# This code is contributed by Danish Raza
C#
// C# program to find LCM
// of two numbers.
using System;
class GFG {
// Recursive method to
// return gcd of a and b
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// method to return
// LCM of two numbers
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
// Driver method
public static void Main()
{
int a = 15, b = 20;
Console.WriteLine("LCM of " + a +
" and " + b + " is " + lcm(a, b));
}
}
// This code is contributed by anuj_67.
JavaScript
<script>
// Javascript program to find LCM of two numbers
// Recursive function to return gcd of a and b
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to return LCM of two numbers
function lcm(a, b)
{
return (a / gcd(a, b)) * b;
}
// Driver program to test above function
let a = 15, b = 20;
document.write("LCM of " + a + " and "
+ b + " is " + lcm(a, b));
// This code is contributed by Mayank Tyagi
</script>
PHP
<?php
// PHP program to find LCM of two numbers
// Recursive function to
// return gcd of a and b
function gcd( $a, $b)
{
if ($a == 0)
return $b;
return gcd($b % $a, $a);
}
// Function to return LCM
// of two numbers
function lcm( $a, $b)
{
return ($a / gcd($a, $b)) * $b;
}
// Driver Code
$a = 15;
$b = 20;
echo "LCM of ",$a, " and "
,$b, " is ", lcm($a, $b);
// This code is contributed by anuj_67.
?>
OutputLCM of 15 and 20 is 60
Time Complexity: O(log(min(a,b))
Auxiliary Space: O(log(min(a,b))
Similar Reads
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E
4 min read
Program to Find GCD or HCF of Two Numbers Given two numbers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4, 5, 10 an
15+ min read
Check if two numbers are co-prime or not Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples : Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-PrimeThe idea is simple, we find GCD of two numbers a
5 min read
GCD of more than two (or array) numbers Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Program to find LCM of two numbers LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 12, b = 18Output : 3636 is the smallest number divisible by both 12 and 18Input : a = 5, b = 11Output : 5555 is the smallest number divisible by both 5 and 11[Naive Approach] Using Conditional Loop This appro
8 min read
LCM of given array elements In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read
Find the other number when LCM and HCF given Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60
4 min read
Minimum insertions to make a Co-prime array Given an array of N elements, find the minimum number of insertions to convert the given array into a co-prime array. Print the resultant array also.Co-prime Array : An array in which every pair of adjacent elements are co-primes. i.e, gcd(a, b) = 1 . Examples : Input : A[] = {2, 7, 28}Output : 1Exp
6 min read
Find the minimum possible health of the winning player Given an array health[] where health[i] is the health of the ith player in a game, any player can attack any other player in the game. The health of the player being attacked will be reduced by the amount of health the attacking player has. The task is to find the minimum possible health of the winn
4 min read
Minimum squares to evenly cut a rectangle Given a rectangular sheet of length l and width w. we need to divide this sheet into square sheets such that the number of square sheets should be as minimum as possible.Examples: Input :l= 4 w=6 Output :6 We can form squares with side of 1 unit, But the number of squares will be 24, this is not min
4 min read