The Wayback Machine - https://web.archive.org/web/20240614183527/https://www.geeksforgeeks.org/lcm-of-two-numbers-in-c/
Open In App

LCM of Two Numbers in C

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to write a C program to find the LCM of two numbers. LCM (Least Common Multiple) of two numbers is the smallest positive number that can be divided by both numbers without leaving a remainder. For example, the LCM of 15 and 25 is 75.

LCM of two number in C

Algorithm to Find LCM in C

  • Find the maximum of the two numbers and store them in a variable max.
  • Run a loop and check if max is divisible by both numbers.
    • if (max % x == 0 && max % y == 0)
  • If the condition is true, it means that max is the LCM of the two numbers.
  • If the condition is false, increment max by 1 and continue the loop to check the next number.

C Program To Find LCM of Two Numbers

C




// C program to find LCM of
// two numbers
#include <stdio.h>
  
// Driver code
int main()
{
    int x = 15, y = 25, max;
    max = (x > y) ? x : y;
  
    // While loop to check if max variable
    // is divisible by x and y
    while (1) {
        if (max % x == 0 && max % y == 0) {
            printf("The LCM of %d and %d is %d.", x, y,
                   max);
            break;
        }
  
        ++max;
    }
  
    return 0;
}


Output

The LCM of 15 and 25 is 75.

Complexity Analysis

  • Time complexity: O(x*y)
  • Auxiliary space: O(1)

LCM of Two Numbers using GCD

In mathematics, the LCM of two numbers is the product of two numbers divided by their GCD. So,

LCM(a, b) = (a x b)  /  GCD(a, b)

Refer to the article Program to Find GCD or HCF of Two Numbers to learn how to find the GCD of two numbers.

C Program To Find LCM of Two Numbers using GCD

C++




// C++ program to find LCM of two numbers
#include <iostream>
using namespace std;
  
// Recursive function to return gcd of a and b
long long gcd(long long int a, long long int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to return LCM of two numbers
long long lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  
// Driver program to test above function
int main()
{
    int a = 15, b = 20;
    cout << "LCM of " << a << " and " << b << " is "
         << lcm(a, b);
    return 0;
}


Output

LCM of 15 and 20 is 60

Complexity Analysis

  • Time complexity: O(log(min(a, b)))
  • Auxiliary space: O(1)

Please refer to the complete article Program to find LCM of two numbers for more methods to find LCM of two numbers.



Similar Reads

Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
Given an integer n, the task is to find the sum: LCM(1, n) + LCM(2, n) + LCM(3, n) + ... + LCM(n, n) where LCM(i, n) is the Least Common Multiple of i and n. Examples: Input: 3 Output: 10 LCM(1, 3) + LCM(2, 3) + LCM(3, 3) = 3 + 6 + 3 = 12 Input: 5 Output: 55 LCM(1, 5) + LCM(2, 5) + LCM(3, 5) + LCM(4, 5) + LCM(5, 5) = 55 Naive Approach: LCM of two n
11 min read
Program to find LCM of two Fibonacci Numbers
Given here are two positive numbers a and b. The task is to print the least common multiple of a'th and b'th Fibonacci Numbers.The first few Fibonacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ......Note that 0 is considered as 0’th Fibonacci Number.Examples: Input : a = 3, b = 12 Output : 144 Input : a = 8, b = 37 Output : 50731415
8 min read
Minimum number to be added to minimize LCM of two given numbers
Given two numbers A and B, the task is to find the minimum number that needs to be added to A and B such that their LCM is minimized. Examples: Input: A = 6, B = 10Output: 2Explanation: On adding 2 to A and B, the value becomes 8 and 12 respectively. LCM of 8 and 12 is 24, which is the minimum LCM possible. Input: A = 5, B = 10Output: 0Explanation:
10 min read
Program to find LCM of two numbers
LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers. For example, LCM of 15 and 20 is 60, and LCM of 5 and 7 is 35. A simple solution is to find all prime factors of both numbers, then find union of all factors present in both numbers. Finally, return the product of elements in union. An efficient
7 min read
Finding LCM of more than two (or array) numbers without using GCD
Given an array of positive integers, find LCM of the elements present in array.Examples: Input : arr[] = {1, 2, 3, 4, 28} Output : 84 Input : arr[] = {4, 6, 12, 24, 30} Output : 120 We have discussed LCM of array using GCD.In this post a different approach is discussed that doesn't require computation of GCD. Below are steps. Initialize result = 1F
9 min read
Program to find LCM of 2 numbers without using GCD
Finding LCM using GCD is explained here but here the task is to find LCM without first calculating GCD.Examples: Input: 7, 5 Output: 35 Input: 2, 6 Output: 6 The approach is to start with the largest of the 2 numbers and keep incrementing the larger number by itself till smaller number perfectly divides the resultant. C/C++ Code // C++ program to f
4 min read
LCM of N numbers modulo M
Given an array arr[] of integers, the task is to find the LCM of all the elements of the array modulo M where M = 109 + 7.Examples: Input: arr[] = {10000000, 12345, 159873} Output: 780789722 LCM of (10000000, 12345, 159873) is 1315754790000000 1315754790000000 % 1000000007 = 780789722Input: arr[] = {10, 13, 15} Output: 390 Approach: If you have gon
11 min read
Highest power of 2 that divides the LCM of first N Natural numbers.
Given a number N, the task is to find the largest power of 2 that divides LCM of first N Natural numbers. Examples: Input: N = 5Output: 2Explanation:LCM of {1, 2, 3, 4, 5} = 6060 is divisible by 22 Input: N = 15 Output: 3Explanation:LCM of {1, 2, 3.....14, 15} = 360360360360 is divisible by 23 Naive Approach: The idea is to find the Least common mu
8 min read
Maximum sum of distinct numbers with LCM as N
Given a number N, the task is to find out the maximum sum of distinct numbers such that the Least Common Multiple of all these numbers is N. Example: Input : N = 12 Output : 28 Maximum sum which we can achieve is, 1 + 2 + 3 + 4 + 6 + 12 = 28 Input : N = 15 Output : 24 We can solve this problem by observing some cases, As N needs to be LCM of all nu
6 min read
Find LCM of rational numbers
Given an array of rational numbers, the task is to find the LCM of these numbers. Examples: Input : vect[] = {2/7, 3/14, 5/3} Output : 30/1 Input : vect[] = {3/14, 5/3} Output : 15/1 Input : vect[] = {3/4, 3/2} Output : 3/2 First find the lcm of all numerator of rational number then find the gcd of all the denominator of rational number then divide
6 min read