Given two positive integers, return their Least Common Multiple (LCM). The LCM of two numbers is the smallest positive integer that is divisible by both. You can use the relationship: LCM(a, b) = (a * b) / GCD(a, b), where GCD is the Greatest Common Divisor found using the Euclidean algorithm. Return 0 if either input is null or zero.

Examples:

Input: a = 4, b = 6 Output: 12 Explanation: LCM(4, 6) = 12 because 12 is the smallest number divisible by both 4 and 6 Input: a = 3, b = 5 Output: 15 Explanation: Since 3 and 5 share no common factors, LCM = 3 * 5 = 15 Input: a = 12, b = 18 Output: 36 Explanation: GCD(12, 18) = 6, so LCM = (12 * 18) / 6 = 36 Input: a = 7, b = 7 Output: 7 Explanation: LCM of a number with itself is that number Input: a = 0, b = 5 Output: 0 Explanation: Zero input returns 0
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay May 2026

Contest runs May 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In