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)/A
Example : A = 15, B = 12 
HCF = 3, LCM = 60 
We can see that 3 * 60 = 15 * 12.

How does this formula work? 
Since HCF divides both the numbers, let. 
A = HCF * x 
B = HCF * y
Note that x and y are not common factors, so LCM must include HCF, x and y. 
So we can conclude. 
LCM = HCF * x * y
So LCM * HCF = HCF * x * y * HCF which is equal to A * B

Below is the implementation of the above approach: 

C++




// CPP program to find other number from given
// HCF and LCM
#include <bits/stdc++.h>
using namespace std;
 
// Function that will calculates
// the zeroes at the end
int otherNumber(int A, int Lcm, int Hcf)
{
    return (Lcm * Hcf) / A;
}
 
// Driver code
int main()
{
    int A = 8, Lcm = 8, Hcf = 1;
 
    // Calling function.
    int result = otherNumber(A, Lcm, Hcf);
 
    cout << "B = " << result;
 
    return 0;
}


Java




// Java program to find other number from given
// HCF and LCM
import java.io.*;
 
class GFG{
 
// Function that will calculates
// the zeroes at the end
static int otherNumber(int A, int Lcm, int Hcf)
{
    return (Lcm * Hcf) / A;
}
 
// Driver code
public static void main(String args[])
{
    int A = 8, Lcm = 8, Hcf = 1;
 
    // Calling function.
    int result = otherNumber(A, Lcm, Hcf);
 
    System.out.println("B = "+ result);
 
}
}


Python3




# Python 3 program to find other
# number from given HCF and LCM
 
# Function that will calculates
# the zeroes at the end
def otherNumber(a, Lcm, Hcf):
    return (Lcm * Hcf) // A
 
# Driver code
A = 8; Lcm = 8; Hcf = 1
 
# Calling function
result = otherNumber(A, Lcm, Hcf)
print("B =", result)
 
# This code is contributed
# by Shrikant13


C#




// C# program to find other number
// from given HCF and LCM
using System;
 
class GFG
{
 
// Function that will calculates
// the zeroes at the end
static int otherNumber(int A, int Lcm,
                              int Hcf)
{
    return (Lcm * Hcf) / A;
}
 
// Driver code
static public void Main(String []args)
{
    int A = 8, Lcm = 8, Hcf = 1;
 
    // Calling function.
    int result = otherNumber(A, Lcm, Hcf);
 
    Console.WriteLine("B = " + result);
}
}
 
// This code is contributed by Arnab Kundu


PHP




<?php
// PHP program to find other number
// from given HCF and LCM
 
// Function that will calculates
// the zeroes at the end
function otherNumber($A, $Lcm, $Hcf)
{
    return ($Lcm * $Hcf) / $A;
}
 
// Driver code
$A = 8; $Lcm = 8; $Hcf = 1;
 
// Calling function.
$result = otherNumber($A, $Lcm, $Hcf);
 
echo "B = " . $result;
 
// This code is contributed
// by Akanksha Rai


Javascript




<script>
 
// Javascript program to find other number from given
// HCF and LCM
 
// Function that will calculates
// the zeroes at the end
function otherNumber(A, Lcm, Hcf)
{
    return (Lcm * Hcf) / A;
}
 
// Driver code
 
    let A = 8, Lcm = 8, Hcf = 1;
 
    // Calling function.
    let result = otherNumber(A, Lcm, Hcf);
 
    document.write("B = " + result);
     
// This code is contributed by Mayank Tyagi
 
</script>


Output

B = 1

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button