0
can anyone explain what is niven number in java
1 Answer
+ 9
A niven or harshad number is a number which is divisible by the sum of its digits.
e.g. 12
sum of digits is 3 (1+2) and 12 is divisible by 3 -> 12 is a niven number
n%10 gives you the last digit of a number. n/10 removes the last digit.
So you could use a while loop to calculate the sum of digits.
Checking for divisibility:
if (number % sum == 0){
...
}
Hope this helps.