LCM of two numbers in C

Find the LCM in C Program: A Comprehensive Guide

The least common multiple (LCM) of two numbers is a basic concept in mathematics and programming. In C programming, to calculate LCM you need a bit of logic and if there are cell loops knowledge or function knowledge. In this tutorial, we are going to learn how to write a c program for finding the LCM.Let us explore our knowledge about the topic more deeply.

What is LCM?

What is Least Common Multiple (LCM)Least Common Multiple (LCM) of two numbers is nothing but the smallest number which will be a part of multiplication table of both the given numbers. For instance, if you take LCM of and 12 and 15 into account, the smallest possible number is updated as 60 because 60 is divided by both 12 and also 15.

LCM Formula and Approach in C

The formula for the LCM is as follow:

LCM(a, b) = (a * b) / GCD(a, b) → Formula for finding LCM of two numbers.

GCD stands for Greatest Common Divisor here. It is easy to compute the LCM from two numbers if you have calculated their GCD.

Program to find LCM in C Solution Steps

Step 1.Include Required headers

First one to include the input output library which is must library for running program.

#include <stdio.h>

Step 2: Write a Function to Find the GCD >> Gcd and Lcm of two numbers in c

First, you will need to get the GCD before we calculate LCM Euclidean algorithm for calculating the GCD Define a function that will output the greatest common divisor (GCD) of two numbers.

int gcd(int a, int b) {
    while (a != b) {
        if (a > b)
            a -= b;
        else
            b -= a;
    }
    return a;
}

Step 3: Write the Function for LCM >> Lcm of two numbers in c

Given the GCD, as described earlier we can calculate the LCM easily.

int lcm(int a, int b) {
    return (a * b) / gcd(a, b);
}

Step 4: Implement the Main Function

In the main function, Input 2 numbers from the user and then pass to lcm function for the output.

int main() {
    int num1, num2;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    printf("LCM of %d and %d is %d", num1, num2, lcm(num1, num2));

    return 0;
}

Complete Program to Find LCM

#include <stdio.h>

int gcd(int a, int b) {
    while (a != b) {
        if (a > b)
            a -= b;
        else
            b -= a;
    }
    return a;
}

int lcm(int a, int b) {
    return (a * b) / gcd(a, b);
}

int main() {
    int num1, num2;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    printf("LCM of %d and %d is %d", num1, num2, lcm(num1, num2));

    return 0;
}

Conclusion

In C, finding the LCM of two numbers using GCD for this is pretty simple. We know that LCM = a*b / GCD, and so by creating a function to compute the gcd you can implement one to calculate the lcm using only this information. This is an efficient method and understandable for a new beginner who learning C programming.

Prime Numbers in C: A Comprehensive Guide

2 thoughts on “LCM of two numbers in C”

Leave a Comment