Write a Java Program to Find GCD(HCF) and LCM of Two Numbers

By | 09.02.2017

Java Program to Find HCF and LCM of Two Numbers


To find the HCF and LCM of two number in Java Programming, you have to ask to the user to enter the two number, to find the HCF and LCM of the given two number to display the value of the HCF and LCM of the two numbers on the output screen as shown in the following program.

 

SOURCE CODE ::

import java.util.Scanner;

public class hcf_lcm
{
    public static void main(String args[])
    {
        int a, b, x, y, t, hcf, lcm;
        Scanner scan = new Scanner(System.in);
                
        System.out.print("Enter Two Number : ");
        x = scan.nextInt();
        y = scan.nextInt();
                
        a = x;
        b = y;
                
        while(b != 0)
        {
            t = b;
            b = a%b;
            a = t;
        }
                
        hcf = a;
        lcm = (x*y)/hcf;
                
        System.out.print("HCF = " +hcf);
        System.out.print("\nLCM = " +lcm);
    }
}

 

OUTPUT ::

 

Enter Two Number : 4
14
HCF = 2
LCM = 28

 

 

1 2 votes
Article Rating
Category: Java Programming Number Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments