Find HCF and LCM - C++ Program

Q. Write a C++ program to find the HCF and LCM of two numbers.

Answer:

HCF stands for Highest Common Factor. It is also known as Greatest Common Divisor (GCD) or Greatest Common Factor (GCF). HCF of two numbers are defined the largest number that divides both numbers completely with remainder zero.

LCM stands for Lowest Common Multiple. LCM of two numbers are defined as smallest number that is multiple of both numbers.

#include<iostream>
using namespace std;
int main()
{
        int a, b, x, y, temp, hcf, lcm;
        cout<<"\n Enter Two Numbers : \n";
        cin>>x>>y;
        a=x;
        b=y;
        while(b!=0)
        {
                temp=b;
                b=a%b;
                a=temp;
        }
        hcf=a;
        lcm=(x*y)/hcf;
        cout<<"\n HCF : "<<hcf<<"\n";
        cout<<"\n LCM : "<<lcm<<"\n";
        return 0;
}


Output:

hcf lcm