Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor) and LCM(Least Common Multiple)

Description

GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest
number that divides both of them. 

A simple solution is to find all prime factors of both numbers, then find
intersection of all factors present in both numbers. Finally return product
of elements in the intersection.

LCM (Least Common Multiple) of two numbers is the smallest number which can
be divided by both numbers.

A simple solution is to find all prime factors of both numbers, then 
find union of all factors present in both numbers. Finally, return the
product of elements in union.

C/C++

/* C Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor)
 and LCM(Least Common Multiple) */
//Save it as CalculateLCMHCF.c

#include<stdio.h>
long int calculateHCF(long int, long int);
int main(){

    long int num1, num2, HCF, LCM;

    printf("Enter first number : ");
    scanf("%ld",&num1);

    printf("Enter second number : ");
    scanf("%ld",&num2);

    HCF = calculateHCF(num1, num2);
    LCM = (num1 * num2)/HCF;

    printf("The HCF of %ld and %ld is %ld",num1, num2, HCF);
    printf("\nThe LCM of %ld and %ld is %ld",num1, num2, LCM);

    return 0;
}

long int calculateHCF(long int num1, long int num2){
    if(num1 == 0) {
        return num2;
    }
    do{
        if(num1>num2){
            num1 = num1 - num2;
        } else{
            num2 = num2 - num1;
         }
    }while(num2 != 0);

    return num1;
}

Output

Input:
Enter first number : 6
Enter second number : 8

Output:
The HCF of 6 and 8 is 2
The LCM of 6 and 8 is 24

Java

/* Java Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor)
 and LCM(Least Common Multiple) */
//Save it as CalculateLCMHCF.java

import java.io.*;
import java.util.Scanner;

public class CalculateLCMHCF {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter first number : ");
        long num1 = scanner.nextLong();
        
        System.out.println("Enter second number : ");
        long num2 = scanner.nextLong();
        
        long HCF = calculateHCF(num1, num2);
        long LCM = (num1 * num2)/HCF;
        
        System.out.println("The HCF of "+num1+" and "+num2+" is "+HCF);
        System.out.println("The LCM of "+num1+" and "+num2+" is "+LCM);
    }

    private static long calculateHCF(long num1, long num2) {
        
        if(num1 == 0) {
            return num2;
        }
        do{
            if(num1>num2){
                num1 = num1 - num2;
            } else{
                num2 = num2 - num1;
             }
        }while(num2 != 0);
            
        return num1;
    }
}
Input:
Enter first number : 
6
Enter second number : 
8

Output:
The HCF of 6 and 8 is 2
The LCM of 6 and 8 is 24

Related Programs

1) Program to find LCM(Least Common Multiple)
2) Program to find HCF using Recursion
3) Program to find factors of a Number
4) Program to calculate factorial using Recursion
5) Program to convert Binary to Hexa Decimal
6) Program to display multiplication table of a number
7) Program to reverse a number
8) Program to add two complex numbers
9) Program to calculate volume and surface area of cylinder
10) Program To Check Armstrong Number
Share Me

Leave a Reply