Write a program to Calculate LCM of given numbers.
Take x and y as input. Write a function that takes in x and y as integer parameters. The function should return the lcm of these two numbers. In the end print the final lcm.
Input Format
first line take integer input as x.
second line take integer input as y.
Constraints
1<=x<=1000
1<=y<=1000
Output Format
print the lcm of given numbers.
Sample Input 0
15
20
Sample Output 0
60
Explanation 0
LCM of 15 and 20 is 60
Also Read: Write a Program to Transform number.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int finalLCM(int x, int y){
int max=y;
if(x > y)
max=x;
//GCD
int GCD=1;
for(int i=1; i<=max; i++){
if(x%i==0 && y%i==0)
GCD = i;
}
int finalLCM = (x*y)/GCD;
return finalLCM;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println(finalLCM(x,y));
}
}