# C++ Program to find the LCM of two numbers

## Program to find the LCM of two numbers

Here we will discuss how to find the LCM of two numbers entered by the user in c++ programming language.

LCM, or least common multiple in mathematics,of two numbers is the smallest positive integer that is divisible by both the numbers.

For a reference let’s find LCM of 6 and 7

• Multiples of 6 are 6, 12, 18, 24, 30, 36, 42, 48…
• Multiples of 7 are 7, 14, 21, 28, 35, 42, 47, 54…

As we can see the lowest or least common multiple of 6 and 7 is 42

There are many approaches to find the LCM of two numbers like recursion, loops, etc. Here we are using the recursion method. ## Algorithm:-

1. The user gives two numbers as inputs.
2. The input is stored in two int type variables say first and second.
3. A function, findLCM is called to find the LCM with first and second as parameters.
1. A static int variable fact is taken so that it only gets initialized once
2. If fact is divisible for both numbers then fact is the LCM

if( fact % first == 0 && fact % second == 0 )

3. Otherwise following happens

1. The value of fact is incremented

fact=fact+first

4. findLCM function is recursively called again

5. A value that is returned is the LCM of two numbers entered by the user

## Code in C++

`//C++ program    //LCM of two numbers    #include<iostream>    using namespace std;    int findLCM(int,int);    //main program    int main()    {            int first,second;            cout<<"Enter first number : ";            cin>>first;            cout<<"Enter second number : ";            cin>>second;            //calling function to find lcm            cout<<findLCM(first,second)<<" is the LCM of two numbers.";            return 0;    }    //function to find lcm    int findLCM(int first, int second)    {          static int fact = first;        // if true then fact is the lcm        if(fact % first == 0 && fact % second == 0)        {            return fact;        }        //if false call function again        else        {            fact=fact + first;            findLCM(first,second);        }        return fact;    }`
`Output:    Enter first number : 8    Enter second number : 14    56 is the LCM of two numbers.` • Highest Common Factor(HCF): C | C++Java | Python
• Lowest Common Multiple (LCM) : C | C++ | Java | Python
• Greatest Common Divisor : C | C++ | Java | Python
• Binary to Decimal to conversion : C | C++ | Java | Python
• Binary to Octal conversion : C | C++ | Java  | Python
• Decimal to Binary conversion: C | C++ | Java | Python
• Decimal to octal Conversion: C | C++ | Java | Python
• Octal to Binary conversion : C | C++ | Java | Python
• Octal to Decimal conversion : C | C++ | Java | Python
• Quadrants in which a given coordinate lies : C | C++ | Java | Python
• Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
• Maximum number of handshakes: C | C++ | Java | Python
• Addition of two fractions: C | C++ | Java | Python
• Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
• Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
• Count possible decoding of a given digit sequence : C | C++ | Java
• Check whether a character is a vowel or consonant : C | C++ | Java | Python
• Check whether a character is a alphabet or not : C | C++ | Java | Python
• Calculate the area of a circle : C | C++ | Java | Python
• Find the ASCII value of a character : C | C++ | Java | Python
• Find the prime numbers between 1 to 100 : C | C++ | Java | Python
• Calculate the number of digits in an integer : C | C++ | Java | Python
• Convert digit/number to words : C | C++ | Java | Python
• Counting number of days in a given month of a year: C | C++ | Java | Python
• Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
• Finding number of integers which has exactly x divisors: C | C++ | Java | Python
• Finding Roots of a quadratic equation : C | C++ | Java | Python