Calculate L.C.M of two number in java Learn how to Calculate L.C.M of two number in java

Learn how to calculate lcm of two number in java language by giving input from the keyboard or not in details with an example:

In this article, you’ll learn to find the lcm of two number in details with more example by using GCD, and by not using GCD. first, we start by using non GCD. In the above diagram, 12, 16 and 36 are the common multiple of 4 and 6, but the least common multiple is 12.

Therefore, L.C.M of 4 and 6 are 12.

Example 1:

 public class lcmExp {   public static void main(String[] args) { int a=4,b=6,lcm; for(lcm=1;lcm<=a*b;lcm++) if( lcm % a == 0 && lcm % b == 0 ) { System.out.printf(“LCM of %d and %d is %d.”, a, b, lcm); break; } } }

 Output of this program: LCM of 4 and 6 is 12.

• Integer Variable a and b will be stored 4 and 6 respectively
• The range of lcm of a and b in between (1 to a*b), for this program the range of a and b (1 to 12)
• We create a loop that starts at 1 and checks up to a*b.
• Then, check the value of lcm % a == 0 && lcm % b == 0, it means if lcm % a and lcm % b  and we get remainder equal to zero in both case
• Then we find the lcm of a and b. In the above picture, we find the lcm equal to 12.

Example 2:

 public class lcmExp {  public static void main(String[] args) { int a=40,b=60,lcm; for(lcm=a>b?a:b;lcm<=a*b;lcm++) if( lcm % a == 0 && lcm % b == 0 ) { System.out.printf(“LCM of %d and %d is %d.”, a, b, lcm); break; } } }

 LCM of 40 and 60 is 120.

Note: This program is more efficient than example1 because in this program loop will be started at grater value of a or b.

Example 3:

 import java.util.Scanner; public class doWhile {  public static void main(String[] args) {   int a=400,b=600,lcm; for(lcm=a>b?a:b;lcm<=a*b;lcm=lcm+(a>b?a:b))    if( lcm % a == 0 && lcm % b == 0 ) { System.out.printf(“LCM of %d and %d is %d.”, a, b, lcm); break; } } }

 LCM of 400 and 600 is 1200.

Note: This program is more efficient than above all because in this program loop will be started at a greater value of a or b and check only the multiple of greater value either a or b.

Example 4: input given by user from the keyboard.

 import java.util.Scanner; public class doWhile { public static void main(String[] args) { System.out.print (“Enter two number: “); Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int b=sc.nextInt(); int lcm = (a > b) ? a: b; for(lcm=a>b?a:b;lcm<=a*b;lcm++) if( lcm % a == 0 && lcm % b == 0 ) { System.out.printf(“LCM of %d and %d is %d.”, a, b, lcm); break; } } }

 Output of this program: Enter two number: 30 40 LCM of 30 and 40 is 120

using GCD

Example 5: input from the keyboard

 public class doWhile { public static void main(String[] args) {   int a = 28, b = 7, gcd = 1,lcm;   for(int i = 1; i <= a && i <= b; ++i) { if(a % i == 0 && b % i == 0) gcd = i; } lcm = (a * b) / gcd; System.out.printf(“The LCM of %d and %d is %d.”, a, b, lcm); } }

 The LCM of 28 and 7 is 28.

Example 6: input from keyboard

 import java.util.Scanner; public class doWhile {   public static void main(String[] args) { System.out.println (“Enter two number”); Scanner sc=new Scanner(System.in);   int a=sc.nextInt();   int b=sc.nextInt(); int lcm = (a > b) ? a: b;   while(true) { if( lcm % a == 0 && lcm % b == 0 ) { System.out.printf(“LCM of %d and %d is %d.”, a, b, lcm); break; } ++lcm; } } }

 Output of this program: Enter two number 55 60 LCM of 55 and 60 is 660