# How To Check Whether a Number is a Harshad Number or Not in Java?

Harshad number can be defined as a number which is divisible by the sum of its digits. Simply it means if the sum of the digits of the number is a factor of that number then it is a Harshad number.

In this article we will see how to check Harshad numbers by using the Java programming language.

## To show you some instances

### Instance-1

Input number is 18

Let’s check it by using the logic of Harshad number −

Sum of the digits of the number = 1 + 8 = 9.

So, 18 is divisible by 9.

Hence, 18 is a Harshad number.

### Instance-2

Input number is 3

Let’s check it by using the logic of Harshad number −

Sum of the digits of the number = 3.

So, 3 is divisible by 3.

Hence, 3 is a Harshad number.

### Instance-3

Input number is 15

Let’s check it by using the logic of Harshad number −

Sum of the digits of the number = 1 + 5 = 6.

So, 15 is not divisible by 6.

Hence, 14 is not a Harshad number.

Some other examples of Harshad numbers include 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20 etc.

## Syntax

To convert integer value to String value by using inbuilt toString() method.

Following is the Syntax to get total number of digits in a number by converting the integer value to String value and then finding its length and assigning the length to an integer variable −

String str = Integer.toString(input_number);


To get the length of the Integer, we will use the Java String class inbuilt length() method which returns the length of the String object.

int length = st.length();


To get the character at a specific position/index in a string we use the charAt() method. Where charAt(i)-‘0’ returns the actual integer value.

int num = st.charAt(i)-‘0’;


Where ‘st’ refers to the string ‘i’ is the iterator variable to iterate the string.

## Algorithm

### Algorithm 1

• Step 1 − Get an integer number either by initialization or by user input.

• Step 2 − By iterating each digit of the number, find the sum of each digit of the number.

• Step 3 − Then check if the original number is divisible by the sum of all digits of the number. If divisible then the given number is a Harshad number else it is not a Harshad number

### Algorithm 2

• Step 1 − Get an integer number either by initialization or by user input.

• Step 2 − Convert that integer to String by using inbuilt toString() method.

• Step 3 − Find the length of the String by using the inbuilt length() method.

• Step 4 − Then by using a for loop, iterate till the length of the String and get one by one integer value from String by using charAt(i)-‘0’ and keep a track on the sum of all digits.

• Step 5 − Then check if the original number is divisible by the sum of all digits of the number. If divisible then the given number is a Harshad number else it is not a Harshad number.

## Multiple Approaches

We have provided the solution in different approaches

• Without Using String

• By Using String

Let’s see the program along with its output one by one.

### Approach-1: Without Using String

In this approach, an integer value will be initialized in the program and then by using the Algorithm-1 we can check whether a number is a Harshad number or not.

#### Example

public class Main{
//main method
public static void main(String args[]){

//Declared an integer variable and initialized a number as value
int originalNumber = 21;

//printing the given number to be checked
System.out.println("Given number: "+originalNumber);

//keep a copy of original number
int copyOfOriginalNumber = originalNumber;

//initialize sum as 0
int sum = 0;

//Find sum of all digits of the number
//continue the loop till the number is greater than 0
while(originalNumber > 0){

//get the rightmost digit of the number by using % operator
int rem = originalNumber%10;

sum = sum + rem;

//remove the rightmost digit from number and get the updated number
originalNumber = originalNumber/10;
}

//printing the result
if(copyOfOriginalNumber % sum == 0)
else
}
}


#### Output

Given number: 21


### Approach-2: By Using String

In this approach, an integer value will be initialized in the program and then by using the Algorithm-2 we will check if the number is a Harshad number or not.

#### Example

public class Main{
//main method
public static void main(String args[]){

//Declared an integer variable and initialized a number as value
int originalNumber = 40;

//printing the given number to be checked
System.out.println("Given number: "+originalNumber);

//keep a copy of original number
int copyOfOriginalNumber = originalNumber;

//initialize sum as 0
int sum = 0;

//convert the integer to string by using toString() method
String str = Integer.toString(originalNumber);

//find length of String by using length() method
//which is nothing but total number of digits in the given number
int length=str.length();

//iterate the String and get the digits by using charAt(i)-'0'
//find the sum of digits
for(int i = 0; i < length; i++){
sum += str.charAt(i)-'0';
}

//printing the result
if(copyOfOriginalNumber % sum == 0)
else
System.out.println(copyOfOriginalNumber+" is not a Harshad number");
}
}


#### Output

Given number: 40