Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
433 B
2
Indexable
Never
// Checks if a number is divisible by sum of its digits
bool is_divisible(long x) {
  // Save initial input
  long temporary = x;

  // Sum all digits of the number
  int sum_digits = 0;
  while (x != 0) {
    int y = (int) x % 10;
    sum_digits = (sum_digits + y);
    x /= 10;  // To next digit
  }

  // Control statement
  if (temporary % sum_digits == 0) {
    return true;
  } else {
    return false;
  }
}