Tuesday, November 4, 2014

Filled Under: , , , ,

Program to find L.C.M. of any two numbers using BufferedReader.

Share


Code Box
---------------------------------------

/*Program to find L.C.M. of any two numbers using BufferedReader */

import java.io.*;
class lcm
{
public static void main(String x[]) throws IOException
{
int num1,num2,a,b,count,ans,i;
ans=1;

BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
System.out.print("\n----------------- L.C.M. Calculator ---------------");
System.out.print("\nEnter first number:");
a=Integer.parseInt(obj.readLine());
System.out.print("\nEnter second number:");
b=Integer.parseInt(obj.readLine());
num1=a;
num2=b;

//prime factors are 2,3,5,7

System.out.print("\nL.C.M. Factors => ");
for(i=7;i>=2;)
{
count=0;

if(num1%i==0)
{
num1=num1/i;
count=1;
}

if(num2%i==0)
{
num2=num2/i;
count=1;
}

if(count==0)
{
i--;
}
else
{
System.out.print("  "+i);
ans=ans*i;
}
}

//in case of prime number
System.out.print(" "+num1+" "+num2);
ans=ans*num1*num2;

System.out.print("\nL.C.M. of "+a+" & "+b+" is : "+ans);
System.out.print("\n\n-------------------------------------------");
}
}

0 comments:

Post a Comment