Skip to main content

Command Palette

Search for a command to run...

HackerRank: Least Common Multiple

LCM, LCD opposite of HCF, HCD, HCM, GCD or GCM

Published
•1 min read•View as Markdown

Given two numbers, find the least common multiple of the two.

Input Format

Two space separated integers

Constraints

None

Output Format

A single integer

Sample Input 0
2 5
Sample Output 0

10
Sample Input 1

7 14
Sample Output 1

14
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here */
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();

        // if( m == n){
        //     System.out.println(m);
        // }
        // if(m ==0 || n ==0){
        //     System.out.println("0");
        // }
        System.out.println( (m*n)/gcd(m,n));
    }
    public static int gcd(int a, int b) {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }

}