Count Numbers Divisible by 3
Count integers divisible by three between one and n with Python. Students separate a match counter from a total and use modulo inside a bounded for loop.
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
In this one
- Filter multiples
- Count rather than sum
- Start a counter at zero
How it works
- 1
Visit one through n.
- 2
Increment count when i % 3 is zero.
- 3
Print count after all candidates are checked.
Edge cases
Is zero considered?
Is zero considered?
No. The interval begins at one.
What if n is two?
What if n is two?
No value passes, so count remains zero.
What's going on
The loop visits every integer from one through n. count increases only when the current integer has remainder zero after division by three.
For n equal to ten, matches are three, six, and nine, so the answer is three. The program counts matching values; it does not add those values. The counter starts at zero so an empty interval naturally reports no matches.
Is zero considered? No. The interval begins at one.
What if n is two? No value passes, so count remains zero.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is added for each match?
Pick an answer to check it.
Question 2
Is zero considered?
Pick an answer to check it.
Questions people ask
Is zero considered?
No. The interval begins at one.
What if n is two?
No value passes, so count remains zero.
What is added for each match?
One.
๐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