What is Harshad (or Niven) Number ?

A number which is completely divisible by the sum of its digits is called a Harshad number. When we say completely it means that the remainder of the division is zero. Harshad numbers are also called Niven numbers.
For Example, Consider 192.

Sum of its digits = 1 + 9 + 2 = 12. 192 is completely divisible by 12. Hence 192 is a Harshad number.

Other examples of Harshad number are : 12, 18, 20…..110, 111, 112….150, 152.. and so on.

All single digit numbers are Harshad numbers.

Java program
Java program which accepts a number and tells if it is a Harshad number or not is given below.

static void checkHarshadNumber() {
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter a number");
   int number = scanner.nextInt();
   //initialize variable which will hold the sum of entered number
   int sum = 0;
   // initialize a variable equal to the number as we can not 
   // modify original number since we will require it later 
   int temp = number;
   // loop to calculate sum of digits of number 
   while (temp > 0) {
      int quotient = temp % 10;
      sum = sum + quotient;
      temp = temp / 10;
   }
   //check if the remainder of division of number by its sum is zero
   if (number % sum == 0) {
	System.out.println(number + " is a Harshad number");
   } else {
 	System.out.println(number + " is not a Harshad number");
   }
   scanner.close();
}

Explanation
The number which is to be checked is taken as an input from the user using Scanner class. Its digits are then summed up. Follow below procedure to determine the sum of digits of the number :

  1. Declare a variable to hold the sum of digits. Initialize it to 0.
  2. Start a loop till the number > 0.
  3. In every iteration, take out modulus of the number by 10. Modulus gives the remainder when a digit is divided by 10.
    For Example, if the number is 432, then its modulus by 10 in first iteration will give 2 as 432 / 10 gives 2 as remainder.
  4. Add this modulus to a number to the variable declared to hold the sum.
  5. Divide the number by 10. Dividing the number gives you the quotient. Assign it to the variable which holds the actual number.
    This will strip off the last digit from the number. For Example, if the number is 432, then its division by 10 in first iteration will give 43 as 432 / 10 gives 43 as quotient.
  6. Steps 3 to 5 are repeated and in every iteration, we get the last digit of the number which is added to the variable holding sum and the number is reduced by 1 digit.
    In the end the quotient becomes zero and the loop terminates.

When the digits of the number are summed up, check that the result of modulus (%) of the number by the sum of its digits is equal to zero.
If yes, then you have got a Harshad number.

In the above example, we have used a temporary variable as a copy of the original number. This has been done since we require the original number later in the program.
If the number is not required after summing its digits, then the loop may be iterated on the original number as well.

Hit the clap if the content was useful.

Leave a Reply