Find LCM Using Loops
Find LCM of two numbers using loops.
IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 5 of 13
find-lcm-using-loops.py
1# Take two numbers2a = int(input("Enter first number: "))3b = int(input("Enter second number: "))45# Find LCM6larger = max(a, b)7lcm = larger89while True:10 if lcm % a == 0 and lcm % b == 0:11 break12 lcm += larger1314print(f"LCM of {a} and {b} is {lcm}")
Output
Enter first number: 12 Enter second number: 18 LCM of 12 and 18 is 36
What's going on
Find smallest number divisible by both.
Key Concepts:
Start from larger number
Check multiples of larger number
First number divisible by both is LCM
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum