group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
262 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
06 Feb 2019

What is Harshad number or Niven number and how to check given number is niven number?

Harshad number or Niven number is a number which is divisible by the sum of its digits. For example,

1) 21 is a Harshad number because it is divisible by the sum of its digits.

21 –> sum of digits –> 2+1 = 3 and 21 is divisible by 3 –> 21/3 = 7.

2) 111 is a Harshad number because it is divisible by the sum of its digits.

111 –> sum of digits –> 1+1+1 = 3 and 111 is divisible by 3 –> 111/3 = 37

3) 153 is a Harshad number. It is divisible by the sum of its digits.

153 –> sum of its digits –> 1+5+3 = 9 and 153 is divisible by 9 –> 153/9 = 17

Following are the steps of algorithm to find a given number is Harshad number;

Step 1 : Let inputNumber be the input number and sum be the sum of its digits.

Step 2 : First find the sum of digits of inputNumber and store it in sum.

while (inputNumber != 0) 
 {
        lastDigit = inputNumber % 10;
        sum = sum + lastDigit;
        inputNumber = inputNumber / 10;
 }

Step 3 : If inputNumber is divisible by the sum, then inputNumber is a Harshad number.