Count Divisible by 3 & 5

Count elements divisible by both 3 and 5.

Logic BuildingIntermediate
Logic Building
# Take array
n = int(input("Enter array size: "))
arr = []
for i in range(n):
    arr.append(int(input(f"Element {i+1}: ")))

# Count
count = 0
for element in arr:
    if element % 3 == 0 and element % 5 == 0:
        count += 1

print(f"Count: {count}")

Output

Enter array size: 5
Element 1: 15
Element 2: 30
Element 3: 10
Element 4: 20
Element 5: 45
Count: 3

Check both conditions with AND.

Key Concepts:

  • Divisible by 3: element % 3 == 0
  • Divisible by 5: element % 5 == 0
  • Both must be true