VOOZH about

URL: https://www.geeksforgeeks.org/c/lcm-of-two-numbers-in-c/

⇱ LCM of Two Numbers in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

LCM of Two Numbers in C

Last Updated : 23 Jul, 2025

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


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


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.

Comment