can anyone explain what is niven number in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can anyone explain what is niven number in java

21st Jan 2023, 4:09 PM
Yuvraj Kurmi
Yuvraj Kurmi - avatar
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.
21st Jan 2023, 4:25 PM
Denise Roßberg
Denise Roßberg - avatar