Java code to find the LCM i.e. least common factors of given two numbers
import java.io.*;
class Test {
public static void main(String[] args) throws IOException {
int n1,n2,x,y;
System.out.println("Enter first number:");
BufferedReader br1=new BufferedReader(new InputStreamReader (System.in));
n1=Integer.parseInt(br1.readLine());
System.out.println("Enter second number:");
BufferedReader br2=new BufferedReader(new InputStreamReader (System.in));
n2=Integer.parseInt(br2.readLine());
x=n1;
y=n2;
while(n1!=n2){
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
System.out.println("L.C.M of two number is"+x*y/n1);
}
No comments:
Post a Comment