Count Numbers Divisible by 3
Count numbers from 1 to n that are divisible by 3.
BeginnerPhase 2 - Level 1: Basic LoopingExample 11 of 14
count-numbers-divisible-by-3.py
1# Take n as input2n = int(input("Enter n: "))34# Count divisible by 35count = 06for i in range(1, n + 1):7 if i % 3 == 0:8 count += 1910print(f"Numbers divisible by 3: {count}")
Output
Enter n: 20 Numbers divisible by 3: 6
What's going on
Check each number for divisibility by 3.
Key Concepts:
Loop from 1 to n
Check if divisible by 3 using modulo
Count matches
🔗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