C Program to Find LCM of Two Numbers

C Program to Find LCM of Two Numbers

Two school clubs meet every twelve and eighteen days. Find the first positive day when their repeating schedules meet again.

For positive integers, the least common multiple is a divided by gcd(a,b), then multiplied by b.

Before this exercise, review loops and stopping conditions and functions and parameters. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why least common multiple uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use club periods four and six.

Least Common Multiple: follow the data

  1. Given data

    Two school clubs meet every twelve and eighteen days. Find the first positive day when their repeating schedules meet again.

  2. Rule

    For positive integers, the least common multiple is a divided by gcd(a,b), then multiplied by b.

  3. Observed result

    LCM: 36

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

The Euclidean algorithm repeatedly replaces a pair with its second value and the division remainder until the remainder is zero. The remaining first value is the greatest common divisor.

Divide before multiplying to reduce the intermediate product. This reduces overflow risk but does not prove that the final least common multiple fits its type.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
static unsigned gcd(unsigned a, unsigned b) {
  while (b != 0) { unsigned remainder = a % b; a = b; b = remainder; }
  return a;
}
int main(void) {
  unsigned a = 12, b = 18;
  unsigned result = (a / gcd(a, b)) * b;
  printf("LCM: %u\n", result);
  return 0;
}

Expected output

LCM: 36
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
static unsigned gcd(unsigned a, unsigned b) {
  while (b != 0) { unsigned remainder = a % b; a = b; b = remainder; }
  return a;
}
int main(void) {
  unsigned a = 12, b = 18;
  unsigned result = (a / gcd(a, b)) * b;
  printf("LCM: %u\n", result);
  return 0;
}

The pairs for gcd(12,18) become (18,12), (12,6) and (6,0).

The gcd is six, so (12 / 6) × 18 gives 36.

Follow the worked calculation

  1. Start with the sample

    The pairs for gcd(12,18) become (18,12), (12,6) and (6,0).

  2. Apply the rule

    The gcd is six, so (12 / 6) × 18 gives 36.

  3. Check the result

    LCM: 36

    LCM: 36
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
static unsigned gcd(unsigned a, unsigned b) {
  while (b != 0) { unsigned remainder = a % b; a = b; b = remainder; }
  return a;
}
int main(void) {
  unsigned a = 12, b = 18;
  unsigned result = (a / gcd(a, b)) * b;
  printf("LCM: %u\n", result);
  return 0;
}

Use club periods four and six.

Worked answer

The gcd is two. Dividing four by two and multiplying by six gives twelve, the first positive shared meeting interval.

C17 · complete program

#include <stdio.h>
static unsigned gcd(unsigned a, unsigned b) {
  while (b != 0) { unsigned remainder = a % b; a = b; b = remainder; }
  return a;
}
int main(void) {
  unsigned a = 4, b = 6;
  unsigned result = (a / gcd(a, b)) * b;
  printf("LCM: %u\n", result);
  return 0;
}

Expected output

LCM: 12
Run in C compiler →

Recognize the important boundaries

This program’s contract is two positive small unsigned integers. If you allow zero, handle it before dividing by gcd, especially when both values are zero.

Before the final multiplication, compare the reduced factor with the representable maximum divided by b for a general checked implementation.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Does dividing before multiplying guarantee no possible overflow?
Choose one answer. You can try again.

Answer and explanation

No; the final product still must fit.

Correct. The rearrangement only reduces an intermediate value.

Use the idea independently

For positive integers, the least common multiple is a divided by gcd(a,b), then multiplied by b.

Continue with testing the problem’s boundary cases.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Find All Factors of a Number →