Home CPP Examples C++ Program to Find LCM and HCF(GCD) of Two Numbers

C++ Program to Find LCM and HCF(GCD) of Two Numbers

by anupmaurya

Here, you will learn how to find and print LCM and HCF (GCD) of any given two numbers by the user at run-time in C++. Here is the list of programs, you will go through:

  • Find LCM of Two Numbers using while Loop
  • Find HCF of Two Numbers using while Loop
  • Find LCM and HCF of Two numbers using for loop
  • using Function

C++ Find LCM of Two Numbers

To find the LCF of two numbers in C++ programming, you have to ask from user to enter the two number. Then find and print its LCM on output as shown in the program given below.

Note – LCM is the Lowest Common Multiple or Least Common Divisor. For example, if there are two numbers say 10 and 12. Then its Least Common Divisor is 60. That is, both numbers 10 and 12 divides 60 without leaving any remainder (or with leaving 0 as remainder).

// C++ Program to Find LCM of Two Numbers

#include<iostream>
using namespace std;
int main()
{
    int numOne, numTwo, mp;
    cout<<"Enter Two Numbers: ";
    cin>>numOne>>numTwo;
    if(numOne>numTwo)
        mp = numOne;
    else
        mp = numTwo;
    while(1)
    {
        if((mp%numOne == 0) && (mp%numTwo == 0))
            break;
        else
            mp++;
    }
    cout<<"\nLCM ("<<numOne<<", "<<numTwo<<") = "<<mp;
    cout<<endl;
    return 0;
}

Output

Enter Two Numbers: 10
12
LCM(10,12)=60

C++ Find HCF (GCD) of Two Numbers

Unlike LCM, that deals multiple (lowest common), HCM deals with factor (highest common). HCF can be called as Highest Common Factor, or Greatest Common Factor (GCD).

For example, if there are two numbers say 10 and 12, then its highest common factor is 2. That is, 2 is the highest number that divides both the number. 1 also divides both numbers, but 2 is greater, so 2 is HCF of 10 and 12.

The question is, write a program in C++ that finds HCF of two numbers. Here is its answer:

// C++ Program to Find HCF (GCD) of Two Numbers

#include<iostream>
using namespace std;
int main()
{
    int numOne, numTwo, mp;
    cout<<"Enter Two Numbers: ";
    cin>>numOne>>numTwo;
    if(numOne>numTwo)
        mp = numOne;
    else
        mp = numTwo;
    while(1)
    {
        if((numOne%mp == 0) && (numTwo%mp == 0))
            break;
        else
            mp--;
    }
    cout<<"\nHCF ("<<numOne<<", "<<numTwo<<") = "<<mp;
    cout<<endl;
    return 0;
}

Output

Enter Two Numbers: 10
12
HCF(10,12)=2

C++ Find LCM and HCF of Two Numbers using for Loop

Now let’s create the same program using for loop

// Find LCM and HCF of Two Numbers using for Loop

#include<iostream>
using namespace std;
int main()
{
    int numOne, numTwo, mp, temp;
    cout<<"Enter Two Numbers: ";
    cin>>numOne>>numTwo;
    if(numOne>numTwo)
        mp = numOne;
    else
        mp = numTwo;
    temp = mp;
    for(;;mp++)
    {
        if((mp%numOne == 0) && (mp%numTwo == 0))
            break;
    }
    cout<<"\nLCM ("<<numOne<<", "<<numTwo<<") = "<<mp;
    mp = temp;
    for(;;mp--)
    {
        if((numOne%mp == 0) && (numTwo%mp == 0))
            break;
    }
    cout<<"\nHCF ("<<numOne<<", "<<numTwo<<") = "<<mp;
    cout<<endl;
    return 0;
}

Output

Enter Two Numbers: 6
8
LCM(6,8)=24
HCF(6,8)=2

C++ Find LCM and HCF of Two Numbers using Function

This program uses two user-defined functions namely, findLCM() and findHCF() to find LCM and HCF of two entered numbers by user.

Both function receives two arguments, as first and second number, returns LCM/HCF of these two numbers passed as its argument.

// Find LCM and HCF of Two Numbers using Function

#include<iostream>
using namespace std;
int findLCM(int, int);
int findHCF(int, int);
int main()
{
    int numOne, numTwo, res;
    cout<<"Enter Two Numbers: ";
    cin>>numOne>>numTwo;
    res = findLCM(numOne, numTwo);
    cout<<"\nLCM ("<<numOne<<", "<<numTwo<<") = "<<res;
    res = findHCF(numOne, numTwo);
    cout<<"\nHCF ("<<numOne<<", "<<numTwo<<") = "<<res;
    cout<<endl;
    return 0;
}
int findLCM(int a, int b)
{
    int lcm;
    if(a>b)
        lcm = a;
    else
        lcm = b;
    while(1)
    {
        if((lcm%a == 0) && (lcm%b == 0))
            break;
        lcm++;
    }
    return lcm;
}
int findHCF(int a, int b)
{
    int hcf;
    if(a>b)
        hcf = a;
    else
        hcf = b;
    while(1)
    {
        if((a%hcf == 0) && (b%hcf == 0))
            break;
        hcf--;
    }
    return hcf;
}

Output

Enter Two Numbers: 6
8
LCM(6,8)=24
HCF(6,8)=2

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.