Mathematics is a vital part of programming and computer science. It's the core of any good algorithm and it provides the analytical skillset required in programming.

Mathematical algorithms are also a very important topic for programming interviews. In this article, you'll learn how to find GCD and LCM of two numbers using C++, Python, C, and JavaScript.

How to Find the GCD of Two Numbers

The greatest common divisor (GCD) or highest common factor (HCF) of two numbers is the largest positive integer that perfectly divides the two given numbers. You can find the GCD of two numbers using the Euclidean algorithm.

In the Euclidean algorithm, the greater number is divided by the smaller number, then the smaller number is divided by the remainder of the previous operation. This process is repeated until the remainder is 0.

For example, if you want to find the GCD of 75 and 50, you need to follow these steps:

  • Divide the greater number by the smaller number and take the remainder.
        75 % 50 = 25
    
  • Divide the smaller number by the remainder of the previous operation.
        50 % 25 = 0
    
  • Now, the remainder becomes 0, thus the GCD of 75 and 50 is 25.

C++ Program to Find the GCD of Two Numbers

Below is the C++ program to find the GCD of two numbers:

        // C++ program to find GCD/HCF of 2 numbers
#include <iostream>
using namespace std;

// Recursive function to find GCD/HCF of 2 numbers
int calculateGCD(int num1, int num2)
{
 if(num2==0)
 {
 return num1;
 }
 else
 {
 return calculateGCD(num2, num1%num2);
 }
}

// Driver Code
int main()
{
 int num1 = 34, num2 = 22;
 cout << "GCD of " << num1 << " and " << num2 << " is " << calculateGCD(num1, num2) << endl;

 int num3 = 10, num4 = 2;
 cout << "GCD of " << num3 << " and " << num4 << " is " << calculateGCD(num3, num4) << endl;

 int num5 = 88, num6 = 11;
 cout << "GCD of " << num5 << " and " << num6 << " is " << calculateGCD(num5, num6) << endl;

 int num7 = 40, num8 = 32;
 cout << "GCD of " << num7 << " and " << num8 << " is " << calculateGCD(num7, num8) << endl;

 int num9 = 75, num10 = 50;
 cout << "GCD of " << num9 << " and " << num10 << " is " << calculateGCD(num9, num10) << endl;

 return 0;
}

Output:

        GCD of 34 and 22 is 2
GCD of 10 and 2 is 2
GCD of 88 and 11 is 11
GCD of 40 and 32 is 8
GCD of 75 and 50 is 25

Python Program to Find the GCD of Two Numbers

Below is the Python program to find the GCD of two numbers:

Related: What Is Recursion and How Do You Use It?

        # Python program to find GCD/HCF of 2 numbers

def calculateGCD(num1, num2):
    if num2==0:
        return num1
    else:
        return calculateGCD(num2, num1%num2)

# Driver Code
num1 = 34
num2 = 22
print("GCD of", num1, "and", num2, "is", calculateGCD(num1, num2))

num3 = 10
num4 = 2
print("GCD of", num3, "and", num4, "is", calculateGCD(num3, num4))

num5 = 88
num6 = 11
print("GCD of", num5, "and", num6, "is", calculateGCD(num5, num6))

num7 = 40
num8 = 32
print("GCD of", num7, "and", num8, "is", calculateGCD(num7, num8))

num9 = 75
num10 = 50
print("GCD of", num9, "and", num10, "is", calculateGCD(num9, num10))

Output:

        GCD of 34 and 22 is 2
GCD of 10 and 2 is 2
GCD of 88 and 11 is 11
GCD of 40 and 32 is 8
GCD of 75 and 50 is 25

C Program to Find the GCD of Two Numbers

Below is the C program to find the GCD of two numbers:

        // C program to find GCD/HCF of 2 numbers
#include <stdio.h>

// Recursive function to find GCD/HCF of 2 numbers
int calculateGCD(int num1, int num2)
{
 if(num2==0)
 {
 return num1;
 }
 else
 {
 return calculateGCD(num2, num1%num2);
 }
}

// Driver Code
int main()
{
 int num1 = 34, num2 = 22;
 printf("GCD of %d and %d is %d \⁠⁠n" , num1 , num2, calculateGCD(num1, num2));

 int num3 = 10, num4 = 2;
 printf("GCD of %d and %d is %d \⁠⁠n" , num3 , num4, calculateGCD(num3, num4));
 int num5 = 88, num6 = 11;
 printf("GCD of %d and %d is %d \⁠⁠n" , num5 , num6, calculateGCD(num5, num6));

 int num7 = 40, num8 = 32;
 printf("GCD of %d and %d is %d \⁠⁠n" , num7 , num8, calculateGCD(num7, num8));

 int num9 = 75, num10 = 50;
 printf("GCD of %d and %d is %d \⁠⁠n" , num9 , num10 , calculateGCD(num9, num10));

return 0;
}

Output:

        GCD of 34 and 22 is 2
GCD of 10 and 2 is 2
GCD of 88 and 11 is 11
GCD of 40 and 32 is 8
GCD of 75 and 50 is 25

JavaScript Program to Find the GCD of Two Numbers

Below is the JavaScript program to find the GCD of two numbers:

        // JavaScript program to find GCD/HCF of 2 numbers

// Recursive function to find GCD/HCF of 2 numbers
function calculateGCD(num1, num2) {
 if(num2==0)
 {
 return num1;
 }
 else
 {
 return calculateGCD(num2, num1%num2);
 }
}

// Driver Code
var num1 = 34, num2 = 22;
document.write("GCD of " + num1 + " and " + num2 + " is " + calculateGCD(num1, num2) + "
");

var num3 = 10, num4 = 2;
document.write("GCD of " + num3 + " and " + num4 + " is " + calculateGCD(num3, num4) + "
");

var num5 = 88, num6 = 11;
document.write("GCD of " + num5 + " and " + num6 + " is " + calculateGCD(num5, num6) + "
");

var num7 = 40, num8 = 32;
document.write("GCD of " + num7 + " and " + num8 + " is " + calculateGCD(num7, num8) + "
");

var num9 = 75, num10 = 50;
document.write("GCD of " + num9 + " and " + num10 + " is " + calculateGCD(num9, num10) + "
");

Output:

        GCD of 34 and 22 is 2
GCD of 10 and 2 is 2
GCD of 88 and 11 is 11
GCD of 40 and 32 is 8
GCD of 75 and 50 is 25

How to Find the LCM of Two Numbers

The least common multiple (LCM) of two numbers is the smallest positive integer that is perfectly divisible by the two given numbers. You can find the LCM of two numbers using the following mathematical formula:

        num1 * num2 = LCM(num1, num2) * GCD(num1, num2)

LCM(num1, num2) = (num1 * num2) / GCD(num1, num2)

To find the LCM of two numbers programmatically, you need to use the function to find the GCD of two numbers.

Related: How to Add and Subtract Two Matrices in C++, Python, and JavaScript

C++ Program to Find the LCM of Two Numbers

Below is the C++ program to find the LCM of two numbers:

        // C++ program to find LCM of 2 numbers
#include <iostream>
using namespace std;

// Recursive function to find LCM of 2 numbers
int calculateGCD(int num1, int num2)
{
 if(num2==0)
 {
 return num1;
 }
 else
 {
 return calculateGCD(num2, num1%num2);
 }
}

int calculateLCM(int num1, int num2)
{
 return (num1 / calculateGCD(num1, num2)) * num2;
}

// Driver Code
int main()
{
 int num1 = 34, num2 = 22;
 cout << "LCM of " << num1 << " and " << num2 << " is " << calculateLCM(num1, num2) << endl;

 int num3 = 10, num4 = 2;
 cout << "LCM of " << num3 << " and " << num4 << " is " << calculateLCM(num3, num4) << endl;

 int num5 = 88, num6 = 11;
 cout << "LCM of " << num5 << " and " << num6 << " is " << calculateLCM(num5, num6) << endl;

 int num7 = 40, num8 = 32;
 cout << "LCM of " << num7 << " and " << num8 << " is " << calculateLCM(num7, num8) << endl;

 int num9 = 75, num10 = 50;
 cout << "LCM of " << num9 << " and " << num10 << " is " << calculateLCM(num9, num10) << endl;

 return 0;
}

Output:

        LCM of 34 and 22 is 374
LCM of 10 and 2 is 10
LCM of 88 and 11 is 88
LCM of 40 and 32 is 160
LCM of 75 and 50 is 150

Python Program to Find the LCM of Two Numbers

Below is the Python program to find the LCM of two numbers:

        # Python program to find LCM of 2 numbers

def calculateGCD(num1, num2):
    if num2==0:
        return num1
    else:
        return calculateGCD(num2, num1%num2)

def calculateLCM(num1, num2):
    return (num1 // calculateGCD(num1, num2)) * num2

# Driver Code
num1 = 34
num2 = 22
print("LCM of", num1, "and", num2, "is", calculateLCM(num1, num2))

num3 = 10
num4 = 2
print("LCM of", num3, "and", num4, "is", calculateLCM(num3, num4))

num5 = 88
num6 = 11
print("LCM of", num5, "and", num6, "is", calculateLCM(num5, num6))

num7 = 40
num8 = 32
print("LCM of", num7, "and", num8, "is", calculateLCM(num7, num8))

num9 = 75
num10 = 50
print("LCM of", num9, "and", num10, "is", calculateLCM(num9, num10))

Output:

        LCM of 34 and 22 is 374
LCM of 10 and 2 is 10
LCM of 88 and 11 is 88
LCM of 40 and 32 is 160
LCM of 75 and 50 is 150

C Program to Find the LCM of Two Numbers

Below is the C program to find the LCM of two numbers:

        // C program to find LCM of 2 numbers
#include <stdio.h>

// Recursive function to find LCM of 2 numbers
int calculateGCD(int num1, int num2)
{
 if(num2==0)
 {
 return num1;
 }
 else
 {
 return calculateGCD(num2, num1%num2);
 }
}

int calculateLCM(int num1, int num2)
{
 return (num1 / calculateGCD(num1, num2)) * num2;
}

// Driver Code
int main()
{
 int num1 = 34, num2 = 22;
 printf("LCM of %d and %d is %d \⁠n" , num1 , num2, calculateLCM(num1, num2));

 int num3 = 10, num4 = 2;
 printf("LCM of %d and %d is %d \⁠n" , num3 , num4, calculateLCM(num3, num4));
 
 int num5 = 88, num6 = 11;
 printf("LCM of %d and %d is %d \⁠n" , num5 , num6, calculateLCM(num5, num6));

 int num7 = 40, num8 = 32;
 printf("LCM of %d and %d is %d \⁠n" , num7 , num8, calculateLCM(num7, num8));

 int num9 = 75, num10 = 50;
 printf("LCM of %d and %d is %d \⁠n" , num9 , num10 , calculateLCM(num9, num10));

 return 0;
}

Output:

        LCM of 34 and 22 is 374
LCM of 10 and 2 is 10
LCM of 88 and 11 is 88
LCM of 40 and 32 is 160
LCM of 75 and 50 is 150

JavaScript Program to Find the LCM of Two Numbers

Below is the JavaScript program to find the LCM of two numbers:

        // JavaScript program to find LCM of 2 numbers

// Recursive function to find LCM of 2 numbers
function calculateGCD(num1, num2) {
 if(num2==0)
 {
 return num1;
 }
 else
 {
 return calculateGCD(num2, num1%num2);
 }
}

function calculateLCM(num1, num2)
{
 return (num1 / calculateGCD(num1, num2)) * num2;
}

// Driver Code
var num1 = 34, num2 = 22;
document.write("LCM of " + num1 + " and " + num2 + " is " + calculateLCM(num1, num2) + "
");

var num3 = 10, num4 = 2;
document.write("LCM of " + num3 + " and " + num4 + " is " + calculateLCM(num3, num4) + "
");

var num5 = 88, num6 = 11;
document.write("LCM of " + num5 + " and " + num6 + " is " + calculateLCM(num5, num6) + "
");

var num7 = 40, num8 = 32;
document.write("LCM of " + num7 + " and " + num8 + " is " + calculateLCM(num7, num8) + "
");

var num9 = 75, num10 = 50;
document.write("LCM of " + num9 + " and " + num10 + " is " + calculateLCM(num9, num10) + "
");

Output:

        LCM of 34 and 22 is 374
LCM of 10 and 2 is 10
LCM of 88 and 11 is 88
LCM of 40 and 32 is 160
LCM of 75 and 50 is 150

Learn More About Mathematical Algorithms

Mathematical algorithms play a vital role in programming. It's wise to know about some of the basic programs based on mathematical algorithms like Sieve Algorithms, Prime Factorization, Divisors, Fibonacci Numbers, nCr Computations, etc.

Currently, functional programming is at the top of programming trends on the internet. The functional programming paradigm treats computing like mathematical functions and this concept is very useful in programming. You must know about functional programming and which programming languages support it to be the most efficient programmer you can be.