recent

Write a Java program to find out the HCF and LCF

Java program to find the HCF(Highest Common Factor) & LCM(Least Common Multiple) of two numbers:-
  1.  Accept two numbers from user.
  2. Using method of recursion, find the HCF.
  3.  Find the LCM by multiplying the numbers and dividing by their HCF.
  4.  Print HCF and LCM.
import java.util.*;
class hcf
{
public static void main(String args[])
{
int a,b;
Scanner sc= new Scanner(System.in);
System.out.println("Enter two nos :");
a=sc.nextInt();
b=sc.nextInt();
int big;
int small;
if(a>b)
{
big=a;
small=b;
}
else
{
big=b;
small=a;
}
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;
}
}
int temp=1;
while(temp!=0)
{
temp=big%small;
if(temp==0)
{
System.out.println("GCD is "+small);
}
else
{
big=small;
small=temp;}
}
}
}
Powered by Blogger.