# Java Program to Check Harshad number Learn to write a simple java program to verify if a given number is harshad number or not.

## 1. what is a harshad number

A number is called a harshad number (or niven number) is an integer that is divisible by the sum of its digits. i.e. A number MN is divisible by (M+N).

For example, consider following example of number 40.

```Given number is : 40

Sum of digits : 4 + 0 = 4
Is 40 divisible by 4? Yes. So 40 is harshad number.
```
A number which is a harshad number in every number base is called an all-harshad number, or an all-Niven number. There are only four all-harshad numbers: 1, 2, 4, and 6.

## 2. Algorithm to determine harshad number

To find if a given number is harshad or not –

1. Calculate the sum of each digit present in number.
2. Divide the number with sum of digits. If number is divisible with remainder zero, the number i harshad number; else not.

## 3. Java Program to find harshad number

```Main.java
public class Main
{
public static void main(String[] args) {

}

{
int temp = numberToCheck;
int sumOfDigits = 0;
while (temp > 0) {
long rem = temp % 10;
sumOfDigits += rem;
temp = temp / 10;
}

return numberToCheck % sumOfDigits == 0 ? true : false;
}
}
```

Program output.

```Console

```

If you liked this post, share it with all of your programming buddies!

Originally published on https://howtodoinjava.com

## How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

## Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...

## Java File Class Tutorial | Java.io File Class in Java Example

Java File class represents the path of directories and files. It provides the methods for renaming, deleting, and obtaining the properties of file or directory.

## Best Ways To Improve Skills In Java and Python Programming

Join CETPA, India's best institute for Java online training and Certification Course. Enroll now to learn Core to Advanced Java & avail 50% discount on Java Online Course with Certificate.

## Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners