Calculate the Least Common Multiple (LCM) of two positive integers. The LCM is the smallest positive integer that is divisible by both numbers. Use the relationship LCM(a,b) = (a * b) / GCD(a,b) where GCD is the Greatest Common Divisor. Return null for invalid inputs (negative numbers, zero, or null).
Examples:
Input: a = 4, b = 6
Output: 12
Explanation: 12 is the smallest number divisible by both 4 and 6
Input: a = 3, b = 5
Output: 15
Explanation: 3 and 5 are coprime, so LCM is their product
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 identical numbers is the number itself
Input: a = 0, b = 5
Output: null
Explanation: Zero is invalid input
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 accountHow 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 Feb 2026
Contest runs February 1 - 28. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.