Computer Science, asked by BrainlyPromoter, 1 year ago

Write a program in Java to accept two numbers and find the LCM of the two numbers.

( Chapter : Looping Control Structures )


Anonymous: I want help from you can you?
Anonymous: yes
Anonymous: Im not asking from you... bro
BrainlyPromoter: Yes?
Anonymous: please make video on how to delete comments
BrainlyPromoter: Thanks s lot for commenting!

I would like to inform you that comments of any user cannot be deleted uselessly or on request. Comments are deleted in extreme cases when they violate Brainly rules.

*Hope you liked the video*
Anonymous: thanks for responding
siddhartharao77: Kindly dont report the correct answers @anshu9714.
Anonymous: according to the rules we can't attach any screenshot..
siddhartharao77: This is computer science question..In order to show the output, we have to attach.

Answers

Answered by siddhartharao77
9

We know that product of two numbers = HCF * LCM.

Then, LCM = (Product)/HCF.

Sample program to find LCM of two numbers:

import java.util.*;

class Brainly

{

public static void main(String args[])

{

int a,b,c,d,e,f,g;

Scanner demo = new Scanner(System.in);

System.out.println("Enter two numbers :");

a = demo.nextInt();

b = demo.nextInt();


if(a > b)

{

c = a;

d = b;

}

else

{

c = b;

d = a;

}

e = c % d;

while(e != 0)

{

c = d;

d = e;

e = c % d;

}

f = d;

g = a * b / f;


System.out.println("LCM = " +g);

}

}


Output:

Enter two numbers:

9 24

LCM = 72.


Hope it helps you!

Attachments:
Answered by Anonymous
1

public class LCM {

   public static void main(String[] args) {

       int n1 = 72, n2 = 120, lcm;

       // maximum number between n1 and n2 is stored in lcm

       lcm = (n1 > n2) ? n1 : n2;

       // Always true

       while(true)

       {

           if( lcm % n1 == 0 && lcm % n2 == 0 )

           {

               System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);

               break;

           }

           ++lcm;

       }

   }

}

Similar questions