C# LCM Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is LCM?

The least common multiple (LCM) of two integers is the smallest positive number that is divisible by both.

Remember
lcm(a, b) = smallest positive m such that
            m % a == 0 and m % b == 0

Identity (nonnegative a, b):
  gcd(a, b) × lcm(a, b) = a × b
  ⇒  lcm(a, b) = |a × b| / gcd(a, b)

Example: a = 12, b = 18
  Multiples of 12: 12, 24, 36, 48, …
  Multiples of 18: 18, 36, 54, …
  First shared = 36
  Check: gcd(12, 18) = 6 → 12 × 18 / 6 = 36
abgcdlcm
1218636
75135
015150

How to Find LCM in C#

Four clear steps — prefer the GCD formula in interviews and real code.

  1. Take absolute values so negatives do not change the LCM.
  2. If either value is 0, return 0.
  3. Compute gcd(a, b) with Euclid’s algorithm.
  4. Return |a / gcd × b| (divide before multiply to reduce overflow risk).
MethodIdeaTimeBest for
GCD formula|a / gcd × b|O(log min)Interviews, production
Multiple scanIncrease by max(a, b) until both divideO(lcm / max)Teaching the definition

Pseudocode

Pseudocode
function Gcd(a, b):
    a = abs(a), b = abs(b)
    while b != 0:
        t = b
        b = a mod b
        a = t
    return a

function Lcm(a, b):
    a = abs(a), b = abs(b)
    if a == 0 or b == 0:
        return 0
    g = Gcd(a, b)
    return (a / g) * b

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
MeaningSmallest positive multiple of both
Formulalcm = |a × b| / gcd(a, b)
Safe orderDivide by gcd first, then multiply
Zerolcm(0, n) = 0
Three numberslcm(lcm(a, b), c)
Classic checklcm(12, 18) = 36

Live Preview

Enter two integers to see their GCD and LCM.

Whole numbers (absolute values used). Tap a chip or type a pair.

Result
a = 12, b = 18
gcd(12, 18) = 6
lcm = |12 × 18| / 6 = 36

Worked Walkthrough — a = 12, b = 18

Find the GCD first, then apply the LCM formula.

Calculation
Euclid gcd(12, 18):
  18 = 12 × 1 + 6
  12 = 6 × 2 + 0
  gcd = 6

lcm = |12 × 18| / 6
    = 216 / 6
    = 36

Safe form: (12 / 6) × 18 = 2 × 18 = 36

Check: 36 % 12 = 0 and 36 % 18 = 0 ✓

C# Programs

Start with the GCD formula — fast and interview-ready. Use the multiple scan when you want to show the definition directly.

Example 1 — GCD Formula

Compute Euclid’s GCD, then return |a / gcd × b|.

C#
using System;

class Program
{
    static long Gcd(long a, long b)
    {
        a = Math.Abs(a);
        b = Math.Abs(b);
        while (b != 0)
        {
            long t = b;
            b = a % b;
            a = t;
        }
        return a;
    }

    static long Lcm(long a, long b)
    {
        a = Math.Abs(a);
        b = Math.Abs(b);
        if (a == 0 || b == 0)
        {
            return 0;
        }
        return (a / Gcd(a, b)) * b;
    }

    static void Main()
    {
        Console.WriteLine("LCM = " + Lcm(12, 18));
    }
}

How It Works

  1. Take absolute values so sign does not matter.
  2. If either side is 0, return 0.
  3. Gcd repeats a % b until the remainder is 0.
  4. For 12 and 18, gcd is 6.
  5. (12 / 6) * 18 = 36 — divide first to keep the product smaller.

Example 2 — Multiple Scan

Start from the larger number and step by that size until both divide evenly.

C#
using System;

class Program
{
    static long Lcm(long a, long b)
    {
        a = Math.Abs(a);
        b = Math.Abs(b);
        if (a == 0 || b == 0)
        {
            return 0;
        }

        long step = Math.Max(a, b);
        long candidate = step;
        while (true)
        {
            if (candidate % a == 0 && candidate % b == 0)
            {
                return candidate;
            }
            candidate += step;
        }
    }

    static void Main()
    {
        Console.WriteLine(Lcm(12, 18)); // 36
        Console.WriteLine(Lcm(7, 5));   // 35
    }
}

How It Works

  1. Handle zeros and absolute values like Example 1.
  2. Set step = max(a, b) — the LCM cannot be smaller than that.
  3. Test candidate; if both divide it, you found the LCM.
  4. Otherwise add step and try again.
  5. Clear for teaching, but slower than the GCD formula for large values.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

lcm(0, n) = 0

Return early so you never divide by a zero GCD path incorrectly.

Overflow

Divide first

Use (a / gcd) * b with long (or BigInteger) for large inputs.

Negatives

Use Math.Abs

LCM is usually reported as a nonnegative value.

Coprime

gcd = 1

Then lcm(a, b) = |a × b| — still correct.

Time and Space Complexity

ProgramTimeExtra space
GCD formulaO(log min(a, b))O(1)
Multiple scanO(lcm / max(a, b))O(1)

Euclid’s GCD dominates the formula method. The scan walks multiples until it hits the LCM.

Key Takeaways

  • Definition: smallest positive number divisible by both inputs.
  • Formula: lcm = |a × b| / gcd(a, b).
  • Safety: divide by gcd before multiplying; use long.
  • Test set: (12, 18) → 36; (7, 5) → 35; (0, 15) → 0.

One line: find gcd, then compute |a / gcd × b|.

Frequently Asked Questions

The least common multiple of a and b is the smallest positive integer divisible by both (for positive a and b).
For nonnegative integers, gcd(a, b) × lcm(a, b) = a × b, so lcm(a, b) = |a × b| / gcd(a, b).
By common programming convention, lcm(0, n) = 0.
Computing |a / gcd × b| keeps intermediate values smaller than a × b first, which helps avoid overflow in fixed-width integers.
Fold pairwise: lcm(lcm(a, b), c).

Next: Leap Year

Check whether a year is a leap year in C#.

Leap year tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful