Menu
Topics Index
...
`

Find The LCM Of Two Numbers


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to calculate the LCM of two numbers.

Input(First Number, Second Number) Output
132, 154 924
56, 80 560
12, 18 36



class CalculateLCM
{
    public static void main(String s[])
    {
        int result = calculateLcm( 14, 28 );
        System.out.println("The Lowest Common Multiple is " + result);

    }


    public static int calculateLcm(int first, int second)
    {
        int result = 0;
        return result;
    }

}
Topic: Java for loops vs Java while loops vs Java do while loops

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

As per the question we need to find the lcm between 2 numbers... so first find the maximum and minimum values between them...then take a loop to repeat from 1 to minimum value.. In loop, find the multiples of maximum number and then find the multiple of maximum number which is divisible by minimum number. Make the first multiple of maximum number as lcm, which is divisible by minimum number and exit the loop using break statement...

Posted by Gopinath Manchikanti    2015-02-02 08:51:00


In this program we are using the following logic:--@>>. If their is an LCM of two numbers, i.e. if they have any common multiple except 1, then it will always be greater than or equal to the maximum number. It cannot be less than it.2.LCM of two numbers is a number which is a multiple of both the numbers.........For Example, if the 2 numbers are 336 and 224, then there LCM is 672 which is both a multiple of the maximum number (336 x 2 = 672) and also a multiple of the minimum number (224 x 3 = 672).[Yes it is true,chek dis out]............3).So since LCM is the multiple of both the maximum and the minimum number, then it won’t be wrong to use the following logic:->>  Find multiples of the maximum number. [Since, LCM is either equal to or greater than the maximum number]..........#-> Check whether this multiple is also a multiple of the minimum number or not, i.e. whether this multiple is divisible by the minimum number or not........... .#-> If it is divisible, then this is our LCM and we have to stop, because though there may be many other such multiples, but we wanted to find the least multiple of both the numbers..............4. Finally, we are printing the LCM as the output

Posted by Asma Mujtaba Khan    2015-02-02 08:53:45


first check which value is greater than
ex:a=14,b=28,a1=a,b1=b
here b  is greater than a thats y
divide b with a if a divide b succesfully
b is lcm return the b
if not add b1 to b
once again divide b with a if divide succesfully lcm is b
if not continue the same procees upto a b divides a

logic;

a=5( varible first),b=3(variable second);
a1=a;
b1=b;
lcm:                                                                               if(a>b)
    {
       while(a%b!=0)
        {a=a+a1;{
       result=a;
    }
   else if(b>a)
   {
        while(b%a!=0)
    {      b=b+b1;}  
        result=b;
    }
   else
   result=a  or b;
}

return result;

ex: 5>3
but 5%3 is not divisible
add 5+5=10
again 10%3   not possible
add 5 with 10 10+5=15
again 15%3 is possible then 15 is lcm

Posted by Goutham Singarapu    2015-02-02 11:45:54


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Second Correct Comment'. The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-03 02:05:49


© meritcampus 2019

All Rights Reserved.

Open In App