Program for LCM of two numbers
LCM stands for "Least Common Multiple." The LCM of two numbers is the smallest positive integer that is a multiple of both numbers.
To find the LCM of two numbers, you can list the multiples of each number and find the smallest multiple that they have in common. Alternatively, you can use the prime factorization method.
Here is an example:
Find the LCM of 12 and 18.
Listing the multiples:
Multiples of 12: 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, ...
Multiples of 18: 18, 36, 54, 72, 90, 108, 126, ...
The smallest multiple that they have in common is 36. Therefore, the LCM of 12 and 18 is 36.
Using the prime factorization method:
12 = 2^2 x 3 18 = 2 x 3^2
To find the LCM, we need to find the highest power of each prime factor that appears in either factorization.
The highest power of 2 that appears is 2^2. The highest power of 3 that appears is 3^2.
Therefore, the LCM of 12 and 18 is 2^2 x 3^2 = 36.
- Program for lcm of two numbers in java
- Program for lcm of two numbers in c++
- Program for lcm of two numbers in c
- Program for lcm of two numbers in golang
- Program for lcm of two numbers in c#
- Program for lcm of two numbers in vb.net
- Program for lcm of two numbers in php
- Program for lcm of two numbers in node js
- Program for lcm of two numbers in typescript
- Program for lcm of two numbers in python
- Program for lcm of two numbers in ruby
- Program for lcm of two numbers in scala
- Program for lcm of two numbers in swift
- Program for lcm of two numbers in kotlin
Program for lcm of two numbers in java
/*
Java program for
Calculate LCM of two numbers
*/
public class Numbers
{
// Recursively find GCD of two given number
public int gcd(int n1, int n2)
{
if (n1 == 0)
{
return n2;
}
return gcd(n2 % n1, n1);
}
public void lcm(int n1, int n2)
{
// Get LCM of two number
int result = (n1 * n2) / gcd(n1, n2);
System.out.println("Numbers : (" + n1 + " " + n2 + ")");
System.out.println("LCM : " + result);
}
public static void main(String[] args)
{
Numbers task = new Numbers();
// Test Case
task.lcm(24, 75);
task.lcm(45, 30);
task.lcm(21, 49);
task.lcm(15, 20);
task.lcm(16, 40);
}
}
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Program for lcm of two numbers in c++
// Include header file
#include <iostream>
// Stdc++11 program for
// Calculate LCM of two numbers
class Numbers
{
// Recursively find GCD of two given number
public:
int gcd(int n1, int n2)
{
if (n1 == 0)
{
return n2;
}
return gcd(n2 % n1, n1);
}
void lcm(int n1, int n2)
{
// Get LCM of two number
int result = (n1 * n2) / gcd(n1, n2);
std::cout << "Numbers : ("
<< n1 << " " << n2 << ")"
<< std::endl;
std::cout << "LCM : "
<< result << std::endl;
}
};
int main(int argc, char **argv){
Numbers* task = new Numbers();
// Test Case
task->lcm(24, 75);
task->lcm(45, 30);
task->lcm(21, 49);
task->lcm(15, 20);
task->lcm(16, 40);
return 0;
};
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Program for lcm of two numbers in c
// Include header file
#include <stdio.h>
// C program for
// Calculate LCM of two numbers
// Recursively find GCD of two given number
int gcd(int n1, int n2) {
if (n1 == 0) {
return n2;
}
return gcd(n2 % n1, n1);
}
void lcm(int n1, int n2) {
// Get LCM of two number
int result = (n1 * n2) / gcd(n1, n2);
printf("Numbers : (%d,%d)\n", n1, n2);
printf("LCM : %d\n", result);
}
int main() {
// Test Case
lcm(24, 75);
lcm(45, 30);
lcm(21, 49);
lcm(15, 20);
lcm(16, 40);
return 0;
}
Output
Numbers : (24,75)
LCM : 600
Numbers : (45,30)
LCM : 90
Numbers : (21,49)
LCM : 147
Numbers : (15,20)
LCM : 60
Numbers : (16,40)
LCM : 80
Program for lcm of two numbers in golang
package main
import "fmt"
// Golang program for
// Calculate LCM of two numbers
// Recursively find GCD of two given number
func gcd(n1, n2 int)int {
if (n1 == 0) {
return n2;
}
return gcd(n2 % n1, n1);
}
func lcm(n1, n2 int) {
// Get LCM of two number
var result int = (n1 * n2) / gcd(n1, n2);
fmt.Printf("Numbers : (%d, %d)\n",n1,n2);
fmt.Printf("LCM : %d\n",result);
}
func main() {
// Test Case
lcm(24, 75);
lcm(45, 30);
lcm(21, 49);
lcm(15, 20);
lcm(16, 40);
}
Output
Numbers : (24,75)
LCM : 600
Numbers : (45,30)
LCM : 90
Numbers : (21,49)
LCM : 147
Numbers : (15,20)
LCM : 60
Numbers : (16,40)
LCM : 80
Program for lcm of two numbers in c#
// Include namespace system
using System;
// C# program for
// Calculate LCM of two numbers
public class Numbers
{
// Recursively find GCD of two given number
public int gcd(int n1, int n2)
{
if (n1 == 0)
{
return n2;
}
return this.gcd(n2 % n1, n1);
}
public void lcm(int n1, int n2)
{
// Get LCM of two number
var result = (int)((n1 * n2) / this.gcd(n1, n2));
Console.WriteLine("Numbers : (" + n1 + " " + n2 + ")");
Console.WriteLine("LCM : " + result);
}
public static void Main(String[] args)
{
var task = new Numbers();
// Test Case
task.lcm(24, 75);
task.lcm(45, 30);
task.lcm(21, 49);
task.lcm(15, 20);
task.lcm(16, 40);
}
}
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Program for lcm of two numbers in vb.net
' Include namespace system
Imports System
' Vb.net program for
' Calculate LCM of two numbers
public Class Numbers
' Recursively find GCD of two given number
Public Function gcd(ByVal n1 As Integer,
ByVal n2 As Integer) As Integer
if (n1 = 0) Then
Return n2
End If
Return Me.gcd(n2 Mod n1, n1)
End Function
Public Sub lcm(ByVal n1 As Integer, ByVal n2 As Integer)
' Get LCM of two number
Dim result As Integer = ((n1 * n2) / Me.gcd(n1, n2))
Console.WriteLine("Numbers : ({0},{1})",n1,n2)
Console.WriteLine("LCM : {0}",result)
End Sub
Public Shared Sub Main(ByVal args As String())
Dim task As Numbers = New Numbers()
' Test Case
task.lcm(24, 75)
task.lcm(45, 30)
task.lcm(21, 49)
task.lcm(15, 20)
task.lcm(16, 40)
End Sub
End Class
Output
Numbers : (24,75)
LCM : 600
Numbers : (45,30)
LCM : 90
Numbers : (21,49)
LCM : 147
Numbers : (15,20)
LCM : 60
Numbers : (16,40)
LCM : 80
Program for lcm of two numbers in php
<?php
// Php program for
// Calculate LCM of two numbers
class Numbers
{
// Recursively find GCD of two given number
function gcd($n1, $n2)
{
if ($n1 == 0)
{
return $n2;
}
return $this->gcd($n2 % $n1, $n1);
}
function lcm($n1, $n2)
{
// Get LCM of two number
$result = (int)(($n1 * $n2) /
$this->gcd($n1, $n2));
echo "Numbers : ($n1,$n2)\n";
echo "LCM : $result \n";
}
}
$task = new Numbers();
// Test Case
$task->lcm(24, 75);
$task->lcm(45, 30);
$task->lcm(21, 49);
$task->lcm(15, 20);
$task->lcm(16, 40);
Output
Numbers : (24,75)
LCM : 600
Numbers : (45,30)
LCM : 90
Numbers : (21,49)
LCM : 147
Numbers : (15,20)
LCM : 60
Numbers : (16,40)
LCM : 80
Program for lcm of two numbers in node js
// Node Js program for
// Calculate LCM of two numbers
class Numbers
{
// Recursively find GCD of two given number
gcd(n1, n2)
{
if (n1 == 0)
{
return n2;
}
return this.gcd(n2 % n1, n1);
}
lcm(n1, n2)
{
// Get LCM of two number
var result = parseInt((n1 * n2) /
this.gcd(n1, n2));
console.log("Numbers : (" + n1 + " " + n2 + ")");
console.log("LCM : " + result);
}
}
// Start program execution
var task = new Numbers();
// Test Case
task.lcm(24, 75);
task.lcm(45, 30);
task.lcm(21, 49);
task.lcm(15, 20);
task.lcm(16, 40);
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Program for lcm of two numbers in typescript
// Typescript program for
// Calculate LCM of two numbers
class Numbers
{
// Recursively find GCD of two given number
public number gcd(n1:number, n2:number)
{
if (n1 == 0)
{
return n2;
}
return this.gcd(n2 % n1, n1);
}
public lcm(n1:number, n2:number)
{
// Get LCM of two number
var result = parseInt((n1 * n2) /
this.gcd(n1, n2));
console.log("Numbers : (" + n1 + " " + n2 + ")");
console.log("LCM : " + result);
}
}
var task = new Numbers();
// Test Case
task.lcm(24, 75);
task.lcm(45, 30);
task.lcm(21, 49);
task.lcm(15, 20);
task.lcm(16, 40);
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Program for lcm of two numbers in python
# Python 3 program for
# Calculate LCM of two numbers
class Numbers :
# Recursively find GCD of two given number
def gcd(self, n1, n2) :
if (n1 == 0) :
return n2
return self.gcd(n2 % n1, n1)
def lcm(self, n1, n2) :
# Get LCM of two number
result = int((n1 * n2) / self.gcd(n1, n2))
print("Numbers : (" + str(n1) +
" " + str(n2) + ")")
print("LCM :",result)
if __name__=="__main__":
task = Numbers()
# Test Case
task.lcm(24, 75)
task.lcm(45, 30)
task.lcm(21, 49)
task.lcm(15, 20)
task.lcm(16, 40)
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Program for lcm of two numbers in ruby
# Ruby program for
# Calculate LCM of two numbers
class Numbers
# Recursively find GCD of two given number
def gcd( n1, n2)
if (n1 == 0)
return n2
end
return self.gcd(n2 % n1, n1)
end
def lcm( n1, n2)
# Get LCM of two number
result = (n1 * n2) / self.gcd(n1, n2)
print("Numbers : (" +
n1.to_s + " " + n2.to_s + ")\n")
print("LCM : " + result.to_s,"\n")
end
end
task = Numbers.new()
# Test Case
task.lcm(24, 75)
task.lcm(45, 30)
task.lcm(21, 49)
task.lcm(15, 20)
task.lcm(16, 40)
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Program for lcm of two numbers in scala
// Scala program for
// Calculate LCM of two numbers
class Numbers ()
{
// Recursively find GCD of two given number
def gcd(n1 : Int, n2 : Int) : Int=
{
if (n1 == 0)
{
return n2
}
return gcd(n2 % n1, n1)
}
def lcm(n1 : Int, n2 : Int) : Unit=
{
// Get LCM of two number
var result = (n1 * n2) / gcd(n1, n2)
println("Numbers : (" + n1 + " " + n2 + ")")
println("LCM : " + result)
}
}
object Main
{
def main(args : Array[String]) : Unit=
{
var task = new Numbers()
// Test Case
task.lcm(24, 75)
task.lcm(45, 30)
task.lcm(21, 49)
task.lcm(15, 20)
task.lcm(16, 40)
}
}
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Program for lcm of two numbers in swift
import Foundation
// Swift program for
// Calculate LCM of two numbers
class Numbers
{
// Recursively find GCD of two given number
func gcd(_ n1 : Int, _ n2 : Int) -> Int
{
if (n1 == 0)
{
return n2;
}
return self.gcd(n2 % n1,n1);
}
func lcm(_ n1 : Int, _ n2 : Int)
{
// Get LCM of two number
let result : Int = (n1 * n2) / self.gcd(n1,n2);
print("Numbers : (\(n1),\(n2))");
print("LCM : \(result)");
}
}
let task : Numbers = Numbers();
// Test Case
task.lcm(24,75);
task.lcm(45,30);
task.lcm(21,49);
task.lcm(15,20);
task.lcm(16,40);
Output
Numbers : (24,75)
LCM : 600
Numbers : (45,30)
LCM : 90
Numbers : (21,49)
LCM : 147
Numbers : (15,20)
LCM : 60
Numbers : (16,40)
LCM : 80
Program for lcm of two numbers in kotlin
// Kotlin program for
// Calculate LCM of two numbers
class Numbers {
// Recursively find GCD of two given number
fun gcd(n1 : Int, n2 : Int) : Int
{
if (n1 == 0)
{
return n2;
}
return this.gcd(n2 % n1, n1);
}
fun lcm(n1 : Int, n2 : Int) : Unit
{
// Get LCM of two number
val result : Int = (n1 * n2) / this.gcd(n1, n2);
println("Numbers : (" + n1 + " " + n2 + ")");
println("LCM : " + result);
}
}
fun main(args : Array<String>) : Unit
{
val task : Numbers = Numbers();
// Test Case
task.lcm(24, 75);
task.lcm(45, 30);
task.lcm(21, 49);
task.lcm(15, 20);
task.lcm(16, 40);
}
Output
Numbers : (24 75)
LCM : 600
Numbers : (45 30)
LCM : 90
Numbers : (21 49)
LCM : 147
Numbers : (15 20)
LCM : 60
Numbers : (16 40)
LCM : 80
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment