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++
#include <bits/stdc++.h>
using namespace std;
int otherNumber( int A, int Lcm, int Hcf)
{
return (Lcm * Hcf) / A;
}
int main()
{
int A = 8, Lcm = 8, Hcf = 1;
int result = otherNumber(A, Lcm, Hcf);
cout << "B = " << result;
return 0;
}
|
Java
import java.io.*;
class GFG{
static int otherNumber( int A, int Lcm, int Hcf)
{
return (Lcm * Hcf) / A;
}
public static void main(String args[])
{
int A = 8 , Lcm = 8 , Hcf = 1 ;
int result = otherNumber(A, Lcm, Hcf);
System.out.println( "B = " + result);
}
}
|
Python3
def otherNumber(a, Lcm, Hcf):
return (Lcm * Hcf) / / A
A = 8 ; Lcm = 8 ; Hcf = 1
result = otherNumber(A, Lcm, Hcf)
print ( "B =" , result)
|
C#
using System;
class GFG
{
static int otherNumber( int A, int Lcm,
int Hcf)
{
return (Lcm * Hcf) / A;
}
static public void Main(String []args)
{
int A = 8, Lcm = 8, Hcf = 1;
int result = otherNumber(A, Lcm, Hcf);
Console.WriteLine( "B = " + result);
}
}
|
PHP
<?php
function otherNumber( $A , $Lcm , $Hcf )
{
return ( $Lcm * $Hcf ) / $A ;
}
$A = 8; $Lcm = 8; $Hcf = 1;
$result = otherNumber( $A , $Lcm , $Hcf );
echo "B = " . $result ;
|
Javascript
<script>
function otherNumber(A, Lcm, Hcf)
{
return (Lcm * Hcf) / A;
}
let A = 8, Lcm = 8, Hcf = 1;
let result = otherNumber(A, Lcm, Hcf);
document.write( "B = " + result);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)