Be the first user to complete this post

  • 0
Add to List
Beginner

412. Check if Number is divisible by its digits

Objective: Given a number, write a program to check if the number is divisible by all of its digits separately.

Example:

N = 15
Output: true (15 is divisible by 1 and 5)
N = 123 Output: true (123 is divisible by 1, 2 and 3)
N = 143 Output: false (143 is not divisible by 4 and 3)

Approach:

  • Get all the digits of the given number
  • For each digit check if number%digit==0 (means divisible). 
  • If any of them is non zero return false.
  • If for all digits number%digit==0, return true.

Output:

Number 11 is divisible by its digits: true
Number 12 is divisible by its digits: true
Number 13 is divisible by its digits: false
Number 14 is divisible by its digits: false
Number 15 is divisible by its digits: true
Number 16 is divisible by its digits: false
Number 17 is divisible by its digits: false
Number 18 is divisible by its digits: false
Number 19 is divisible by its digits: false