9️⃣C Program to Find LCM of Two Numbers (With Program and Explanation)

 

๐Ÿ”ข C Program to Find LCM of Two Numbers

๐Ÿ“Œ What is LCM?

LCM (Least Common Multiple) of two numbers is the smallest positive number that is divisible by both numbers.

Example:

  • LCM of 6 and 8 = 24

  • LCM of 4 and 5 = 20


๐Ÿง  Logic Used

To find LCM efficiently, we use this formula:

LCM=Number1×Number2GCD​

So first:

  1. Find GCD (Greatest Common Divisor)

  2. Apply the formula


๐Ÿ’ป C Program to Find LCM of Two Numbers





#include <stdio.h>

int main() { int num1, num2, i, gcd, lcm; printf("Enter two numbers: "); scanf("%d %d", &num1, &num2); // Finding GCD for(i = 1; i <= num1 && i <= num2; i++) { if(num1 % i == 0 && num2 % i == 0) { gcd = i; } } // Calculating LCM lcm = (num1 * num2) / gcd; printf("LCM of %d and %d is %d", num1, num2, lcm); return 0; }

๐Ÿ“ Explanation of the Program

๐Ÿ”น Step 1: Taking Input

scanf("%d %d", &num1, &num2);

This line takes two numbers from the user.


๐Ÿ”น Step 2: Finding GCD

for(i = 1; i <= num1 && i <= num2; i++)
  • Loop runs from 1 to the smaller number.

  • If i divides both numbers, it becomes the GCD.


๐Ÿ”น Step 3: Calculating LCM

lcm = (num1 * num2) / gcd;

Using the formula:

LCM=(num1×num2)÷GCDLCM = (num1 × num2) ÷ GCD

๐Ÿ”น Step 4: Displaying Output

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

▶️ Output Examples

✅ Example 1

Input

Enter two numbers: 6 8

Output

LCM of 6 and 8 is 24


๐Ÿ“Œ Key Points

✔ LCM is always a multiple of both numbers
✔ Using GCD makes the program efficient
✔ Works for all positive integers

Conclusion

In this program, we learned how to calculate the LCM of a Number in C.


๐Ÿ“˜ C Programming Series – What’s Next?

๐Ÿ‘‰ In the next post of this series, we will learn how to Find Largest of Three Numbers with complete explanation and output examples. Stay tuned! ๐Ÿš€


Comments

Popular posts from this blog

⓮C Program to calculate Simple Interest (With Program and Explanation)

⑫ C Program to Swap Two Numbers Without Using a Temporary Variable (With Program and Explanation)