Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if the Sum of Digits of a Number N Divides It
A number is divisible by the sum of its digits when the remainder of dividing the number by its digit sum equals zero. For example, if N = 36, the digit sum is 3 + 6 = 9, and since 36 % 9 = 0, the number 36 is divisible by its digit sum.
Algorithm
The algorithm follows these steps ?
- Extract each digit of the number using modulo operator (% 10)
- Add all digits to get the sum
- Check if the original number is divisible by this sum using modulo operator
- Return true if remainder is 0, false otherwise
Method 1: Using Helper Function
This approach separates the digit sum calculation into a helper function for better code organization ?
def digit_sum(n):
total = 0
while n > 0:
total += n % 10
n //= 10
return total
def check_divisibility(n):
return n % digit_sum(n) == 0
# Test the function
number = 36
if check_divisibility(number):
print(f"{number} is divisible by its digit sum")
else:
print(f"{number} is not divisible by its digit sum")
# Test with another number
number = 42
sum_digits = digit_sum(number)
print(f"Number: {number}, Digit sum: {sum_digits}")
if check_divisibility(number):
print(f"{number} is divisible by its digit sum")
else:
print(f"{number} is not divisible by its digit sum")
36 is divisible by its digit sum Number: 42, Digit sum: 6 42 is divisible by its digit sum
Method 2: Direct Calculation
This approach calculates the digit sum directly in the main logic ?
def is_divisible_by_digit_sum(n):
original_n = n
digit_sum = 0
# Calculate sum of digits
while n > 0:
digit_sum += n % 10
n //= 10
# Check divisibility
return original_n % digit_sum == 0
# Test with multiple numbers
test_numbers = [36, 42, 48, 123, 144]
for num in test_numbers:
if is_divisible_by_digit_sum(num):
print(f"{num} is divisible by its digit sum")
else:
print(f"{num} is not divisible by its digit sum")
36 is divisible by its digit sum 42 is divisible by its digit sum 48 is divisible by its digit sum 123 is not divisible by its digit sum 144 is divisible by its digit sum
How It Works
Let's trace through the algorithm with N = 48 ?
def trace_divisibility_check(n):
print(f"Checking number: {n}")
original_n = n
digit_sum = 0
digits = []
# Extract and sum digits
while n > 0:
digit = n % 10
digits.append(digit)
digit_sum += digit
n //= 10
digits.reverse() # Show digits in original order
print(f"Digits: {digits}")
print(f"Sum of digits: {digit_sum}")
print(f"Check: {original_n} % {digit_sum} = {original_n % digit_sum}")
if original_n % digit_sum == 0:
print(f"Result: {original_n} is divisible by {digit_sum}")
else:
print(f"Result: {original_n} is not divisible by {digit_sum}")
trace_divisibility_check(48)
print()
trace_divisibility_check(123)
Checking number: 48 Digits: [4, 8] Sum of digits: 12 Check: 48 % 12 = 0 Result: 48 is divisible by 12 Checking number: 123 Digits: [1, 2, 3] Sum of digits: 6 Check: 123 % 6 = 3 Result: 123 is not divisible by 6
Comparison
| Method | Code Readability | Reusability | Best For |
|---|---|---|---|
| Helper Function | High | High | Multiple operations on digit sum |
| Direct Calculation | Medium | Medium | Single-use scenarios |
Conclusion
To check if a number is divisible by its digit sum, calculate the sum using modulo and integer division operations, then use the modulo operator to check divisibility. Both approaches work effectively, with the helper function method offering better code organization.
