Tuesday, November 4, 2014

Filled Under: , , , ,

Program to find L.C.M. of any two numbers using Command Line argument.

Share
lcm calculator


Code Box

-------------------------------------------
/*Program to find L.C.M. of any two numbers command line argument*/

class lcmc
{
public static void main(String x[]) 
{
int num1,num2,a,b,count,ans,i;
ans=1;

a=Integer.parseInt(x[0]);
b=Integer.parseInt(x[1]);

System.out.print("\n----------------- L.C.M. Calculator ---------------");
System.out.print("\nEntered first number:"+a);
System.out.print("\nEntered second number:"+b);
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