C Program to Find LCM of Two Numbers

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Write a C Program to Find LCM of Two Numbers.

Explanation Video:

Custom Image

Explanation:

 

  • LCM (Least Common Multiple) of two numbers is the smallest number divisible by both.
  • We start from the larger number and increment it until we find the LCM.

Loop Explanation:

Start with max = max(num1, num2).

If max is divisible by both, print LCM and exit.

Else, increment max and check again.

Source Code:
#include <stdio.h>
int main() {
    int num1, num2, max;

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

    max = (num1 > num2) ? num1 : num2;  // Start with the larger number

    while (1) {
        if (max % num1 == 0 && max % num2 == 0) {
            printf("LCM of %d and %d is %d\n", num1, num2, max);
            break;
        }
        max++;  // Try the next number
    }

    return 0;
}
Input:
Enter two numbers: 6 8
Output:
LCM of 6 and 8 is 24
Share this Article & Support Us:
Status
printf('Loading...');