BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Find HCF and LCM of Two Numbers

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, we will write a java program to find the HCF and LCM of two entered numbers.

LCM (Least Common Multiple): The LCM of two numbers is the smallest positive integer which is divisible by both numbers.
HCF (Highest common Factor): HCF is also known as Greatest common divisor, HCF of two numbers is the largest positive integer that divides both the numbers.

Program to Find HCF and LCM of Two Numbers in Java

import java.util.Scanner;

public class JavaExample{
   public static void main(String args[]){
      int temp1, temp2, num1, num2, temp, hcf, lcm;
      Scanner scanner = new Scanner(System.in);

      System.out.print("Enter First Number: ");
      num1 = scanner.nextInt();
      System.out.print("Enter Second Number: ");
      num2 = scanner.nextInt();
      scanner.close();

      temp1 = num1;
      temp2 = num2;

      while(temp2 != 0){
         temp = temp2;
         temp2 = temp1%temp2;
         temp1 = temp;
      }

      hcf = temp1;
      lcm = (num1*num2)/hcf;

      System.out.println("HCF of input numbers: "+hcf);
      System.out.println("LCM of input numbers: "+lcm);
   }
}

Output:
Java Program to Find HCF and LCM of Two Numbers

Related Java Examples

  1. Java Program to find GCD of two numbers
  2. Java Program to add two binary numbers
  3. Java program to swap two numbers
  4. Java program to add two numbers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap