KnowledgeBoat Logo

Computer Applications

Write a program using method name Glcm(int,int) to find the Lowest Common Multiple (LCM) of two numbers by GCD (Greatest Common Divisor) of the numbers. GCD of two integers is calculated by continued division method. Divide the larger number by the smaller, the remainder then divides the previous divisor. The process is repeated till the remainder is zero. The divisor then results in the GCD.
LCM = product of two numbers / GCD

Java

User Defined Methods

56 Likes

Answer

import java.util.Scanner;

public class KboatGlcm
{
    
    public void Glcm(int a, int b) {
        int x = a, y = b;
        while (y != 0) {
            int t = y;
            y = x % y;
            x = t;
        }
        
        int lcm = (a * b) / x;
        
        System.out.println("LCM = " + lcm);
    }
    
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int x = in.nextInt();
        System.out.print("Enter second number: ");
        int y = in.nextInt();
        
        KboatGlcm obj = new KboatGlcm();
        obj.Glcm(x, y);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program using method name Glcm(int,int) to find the Lowest Common Multiple (LCM) of two numbers by GCD (Greatest Common Divisor) of the numbers. GCD of two integers is calculated by continued division method. Divide the larger number by the smaller, the remainder then divides the previous divisor. The process is repeated till the remainder is zero. The divisor then results in the GCD. LCM = product of two numbers / GCD

Answered By

21 Likes


Related Questions