Program to find the LCM of two numbers
Explanation:
In this program we are using the following logic:
1. 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. If you don't believe me, then take any 2 numbers, find their LCM and check whether what I say is true or not.]
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.
That is it. In this program, we have used the above logic only in order to find the LCM.
import java.io.*;
class LcmMethod_2
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b,max,min,x,lcm=1;
System.out.print("Enter the 1st number : ");
a=Integer.parseInt(br.readLine());
System.out.print("Enter the 2nd number : ");
b=Integer.parseInt(br.readLine());
if(a>b)
{
max=a;
min=b;
}
else
{
max=b;
min=a;
}
/*
To find the maximum and minimum numbers, you can also use
int max=a>b?a:b;
int min=a<b?a:b;
*/
for(int i=1;i<=min;i++)
{
x=max*i; //finding multiples of the maximum number
if(x%min==0) //Finding the multiple of maximum number which is divisible by the minimum number.
{
lcm=x; //making the first multiple of maximum number which is divisible by the minimum number, as the LCM
break; //exiting from the loop, as we don't need anymore checking after getting the LCM
}
}
System.out.println("L.C.M. = "+lcm);
}
}
Explanation:
In this program we are using the following logic:
1. 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. If you don't believe me, then take any 2 numbers, find their LCM and check whether what I say is true or not.]
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.
That is it. In this program, we have used the above logic only in order to find the LCM.
import java.io.*;
class LcmMethod_2
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b,max,min,x,lcm=1;
System.out.print("Enter the 1st number : ");
a=Integer.parseInt(br.readLine());
System.out.print("Enter the 2nd number : ");
b=Integer.parseInt(br.readLine());
if(a>b)
{
max=a;
min=b;
}
else
{
max=b;
min=a;
}
/*
To find the maximum and minimum numbers, you can also use
int max=a>b?a:b;
int min=a<b?a:b;
*/
for(int i=1;i<=min;i++)
{
x=max*i; //finding multiples of the maximum number
if(x%min==0) //Finding the multiple of maximum number which is divisible by the minimum number.
{
lcm=x; //making the first multiple of maximum number which is divisible by the minimum number, as the LCM
break; //exiting from the loop, as we don't need anymore checking after getting the LCM
}
}
System.out.println("L.C.M. = "+lcm);
}
}