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.
OUTPUT of the above code
Explanation of the code
Table: Harshad/Niven Number determination examples
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
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) |
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");
}
}
}
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
- Class
HarshadOrNivenNumber
’smain()
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 withSystem.in
i.e.command line
. - The number is input using
Scanner.nextLong()
method which reads the input as along
value, and assigns it to a variable namedinputNo
. - The next section of the code calculates the sum of the digits of
inputNo
. - Value of
inputNo
is stored in a temporary variable namedtemp
.temp
is repeatedly divided by10
(base for decimal numbers) in a while loop till it becomes0
(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 dividingtemp
by10
. This remainder is added to the variablesumOfDigits
which, as its name suggests, stores the sum of individual digits ofinputNo
. - In the last section of the code, check for Harshad number is performed by dividing the
inputNo
by thesumOfDigits
. If the remainder is0
theninputNo
is a Harshad or Niven Number, else it is not.