HCF and LCM
LFPS - Forum :: Study Corner :: Computers :: Java :: Snippets
Page 1 of 1
HCF and LCM
- Code:
/* Program to print HCF and LCM of 2 numbers*/
class HCFandLCM
{
public static void lcm(int a, int b)
{
int big=(a>b)?a:b; //Bigger number
int small=(a>b)?b:a; // smaller number
for(int i=1;i<=big;i++)
{
if(((big*i)%small)==0)
{
int lcm=big*i;
System.out.println("The least common multiple is "+(lcm));
break;
}
}
hcf(big, small); // Arguments sent to hcf() function
}
public static void hcf(int bigC, int smallC)
{
int temp=1;
while(temp!=0)
{
temp=bigC%smallC;
if(temp==0)
{
System.out.println("Highest Common Factor is "+smallC);
}
else
{
bigC=smallC;
smallC=temp;
}
}
}
public static void main(String[] args)
{
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
lcm(x, y);
}
}
Variable Table
Variable Name | Data Type | Description |
a,b,bigC,smallC | int | Formal Parameters |
big, small | int | Used to calculate the hcf and lcm |
x,y | int | Inputs, Actual Parameters |
temp | int | Temporary variable |
LFPS - Forum :: Study Corner :: Computers :: Java :: Snippets
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Feb 14, 2012 9:02 pm by Sujith
» Kolaveri Di
Tue Nov 22, 2011 5:11 pm by Sujith
» Plane of Remembrance
Mon May 23, 2011 7:44 am by Sujith
» Board Exam Results 2011
Fri May 13, 2011 10:19 pm by Sujith
» Source Code
Wed May 04, 2011 8:06 pm by Sujith
» Rio
Wed May 04, 2011 8:04 pm by Sujith
» Fast and Furious 5
Wed May 04, 2011 7:56 pm by Sujith