Java Program to Calculate LCM and HCF of Two Numbers

Java program to calculate LCM (Least Common Multiple) and HCF (Highest common Factor) of two numbers.
import java.util.*;
 class ranjan 
 {
  public static void main(String args[])
  {
    int a,b,c,d,temp,hcf,lcm;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter two numbers : ");
    a=sc.nextInt();
    b=sc.nextInt();
    c=a;
    d=b;
    while(d!=0)
    {	
      temp=d;
      d=c%d;
      c=temp;
    }   
    hcf=c;
    lcm=(a*b)/hcf;
    System.out.println("HCF = "+hcf);
    System.out.println("LCM = "+lcm);
  }
 } 

Output:

Enter two numbers : 6 9
HCF = 3
LCM = 18

Post a Comment

0 Comments