Program to find the LCM of two numbers

 let hcf;


let val1 = Number(prompt('Enter the first number'));

let val2 = Number(prompt('Enter the second number'));


let min = val1 < val2 ? val1 : val2 //ternary operator


for (let i = 1; i <= min; i++) 

{

    if( val1 % i == 0 && val2 % i == 0) 

    {

        hcf = i;

    }

}



let lcm = (val1 * val2) / hcf;



console.log("LCM is " + lcm);


Another Method-


const num1 = Number(prompt('Enter a first positive integer: '));
const num2 = Number(prompt('Enter a second positive integer: '));


let min = (num1 > num2) ? num1 : num2;


while (true) {
    if (min % num1 == 0 && min % num2 == 0) {
        console.log("The LCM is "+ min);
        break;
    }
    min++;
}

Comments

Popular posts from this blog