JB Header
Java program to check if a number is Harshad Number or Niven number
What is a Harshad Number or Niven Number Any positive integer which is divisible by the sum of its digits is a Harshad Number or Niven Number.
Let us see a few examples where we will determine whether the given number is a Harshad Number or not.
Table: Harshad/Niven Number determination examples
Number Sum of digits Is Harshad Number?
2 2 Yes.(2 is divisible by 2)
16 7(6+1) No.(16 is not divisible by 7)
102 3(1+0+2) Yes.(102 is divisible by 3)
152 8(1+5+2) Yes.(152 is divisible by 8)
200 2(2+0+0) Yes.(200 is divisible by 2)
Having understood what a Harshad (or Niven) Number is, let us now see how to implement a program in Java which checks whether the given number is a Harshad Number. Java program to check if a number is a Harshad(or Niven) Number
Java code to determine Harshad or Niven Number
package com.javabrahman.generaljava;
import java.util.Scanner;
public class HarshadOrNivenNumber {

  public static void main(String args[]) {

    //Input number to be tested
    System.out.print("Please enter number to be tested:");
    Scanner scanner = new Scanner(System.in);
    Long inputNo = scanner.nextLong();

    //Calculate sum of digits of inputNo
    long temp = inputNo;
    int sumOfDigits = 0;
    while (temp > 0) {
      long rem = temp % 10;
      sumOfDigits += rem;
      temp = temp / 10;
    }

    //Check if inputNo is divisible by sum of its digits
    if (inputNo % sumOfDigits == 0) {
      System.out.println(inputNo + " is a Harshad/Niven Number");
    } else {
      System.out.println(inputNo + " is NOT a Harshad/Niven Number");
    }

  }
}
 OUTPUT of the above code
Please enter number to be tested: 2
2 is a Harshad/Niven Number

Please enter number to be tested: 16
16 is NOT a Harshad/Niven Number

Please enter number to be tested: 102
102 is a Harshad/Niven Number

Please enter number to be tested: 152
152 is a Harshad/Niven Number

Please enter number to be tested: 200
200 is a Harshad/Niven Number
Explanation of the code
  • Class HarshadOrNivenNumber’s main() method contains the entire logic for Harshad or Niven number determination.
  • First the number to be tested is input using a Scanner instance which is initialized with System.in i.e. command line.
  • The number is input using Scanner.nextLong() method which reads the input as a long value, and assigns it to a variable named inputNo.
  • The next section of the code calculates the sum of the digits of inputNo.
  • Value of inputNo is stored in a temporary variable named temp. temp is repeatedly divided by 10(base for decimal numbers) in a while loop till it becomes 0 (i.e. when there are no digits remaining in the number). With each iteration of the while loop, the last digit is obtained as remainder on dividing temp by 10. This remainder is added to the variable sumOfDigits which, as its name suggests, stores the sum of individual digits of inputNo.
  • In the last section of the code, check for Harshad number is performed by dividing the inputNo by the sumOfDigits. If the remainder is 0 then inputNo is a Harshad or Niven Number, else it is not.