Recursion: Harshad Number

Published by Deep Xavier in

A number is said to be Harshad if it's exactly divisible by the sum of its digits. Create a function that determines whether a number is a Harshad or not.

Examples

isHarshad(75) ➞ false
// 7 + 5 = 12
// 75 is not exactly divisible by 12
 
isHarshad(171) ➞ true
// 1 + 7 + 1 = 9
// 9 exactly divides 171
 
isHarshad(481) ➞ true

isHarshad(89) ➞ false

isHarshad(516) ➞ true

isHarshad(200) ➞ true

Notes

  • You are expected to solve this challenge via recursion.
  • You can check on the Resources tab for more details about recursion in Java.
  • An iterative version of this challenge can be found via this link.
  • A collection of challenges can be found via this link.
Watch a quick demo on how Edabit works.