In this post, we will develop a Java program to check whether the given number is a Harshad number or not? We will also find all Harshad number which belongs to a given range. Later we will develop another Java program to check multiple Harshad numbers.

In mathematics, a Harshad number in a given number base is an integer that is divisible by the sum of its digits when written in that base. The Harshad number is also called a Niven number. It is named after D. R. Kaprekar.

Java program to check the given number is Harshad number or not

``````import java.util.Scanner;

// method to check number is
public static boolean isHarshad(int number) {

// calculate sum of digits of the number
int digitSum = sumOfDigits(number);

if(number % digitSum == 0)
return true;

return false;
}

private static int sumOfDigits(int number) {

int sum = 0;

while(number != 0) {
// add last digit to sum
sum += number%10;

// remove last digit
number /= 10;
}

return sum;
}

public static void main(String[] args) {

// declare variables
int number = 0;
boolean result = false;

//create Scanner class object to take input
Scanner scan = new Scanner(System.in);

// take input from end-user
System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// check number is harshad number or not
if(result)
System.out.println(number +
else
System.out.println(number +
" is not a Harshad number");

// close Scanner class object
scan.close();
}
}``````

The output for the different test-cases:-

Enter an integer number:: 9

Enter an integer number:: 11
11 is not a Harshad number

Enter an integer number:: 378

## Java program to find all Harshad number in a given range

``````import java.util.Scanner;

// method to check number is
public static boolean isHarshad(int number) {

// calculate sum of digits of the number
int digitSum = sumOfDigits(number);

if(number % digitSum == 0)
return true;

return false;
}

// method to find sum of digits
private static int sumOfDigits(int number) {
int sum = 0;

while(number != 0) {
// add last digit to sum
sum += number%10;

// remove last digit
number /= 10;
}

return sum;
}

public static void main(String[] args) {

// declare variables
int minRange = 0, maxRange = 0;

// create Scanner class object
Scanner scan = new Scanner(System.in);

System.out.print("Enter min value of range:: ");
minRange = scan.nextInt();
System.out.print("Enter max value of range:: ");
maxRange = scan.nextInt();

minRange+" to "+ maxRange+" are:: ");

for(int i=minRange; i<=maxRange; i++) {
System.out.print(i+" ");
}

// close Scanner class object
scan.close();
}
}``````

Output:-

Enter min value of range:: 1
Enter max value of range:: 100
The Harshad numbers from 1 to 100 are::
`1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 45 48 50 54 60 63 70 72 80 81 84 90 100`

## Multiple Harshad number in Java

When a Harshad number is divided by the sum of digits and produces another Harshad number then the number is called a Multiple Harshad number. Examples of Multiple Harshad numbers are:- 7, 21, 378, and 6804

Example:-
number = 6804
Sum of digits = 6+8+0+4 = 18
6804 / 18 = 378, so 6804 is a Harshad number.
Now, the sum of digits of 378 = 18
378 / 18 = 21 So, 378 is also a Harshad number.

Hence the number 6804 is a multiple Harshad number.

Java program to check multiple Harshad number

``````import java.util.Scanner;

// method to check number is
public static boolean isHarshad(int number) {

// calculate sum of digits of the number
int digitSum = sumOfDigits(number);

if(number % digitSum == 0)
return true;

return false;
}

// method to find sum of digits
private static int sumOfDigits(int number) {

int sum = 0;
while(number != 0) {
// add last digit to sum
sum += number%10;

// remove last digit
number /= 10;
}

return sum;
}

// method to check number is
// multiple Harshad number or not
public static boolean isMultipleHarshad(int number) {

// first of all check the given
// number is harshad number or not
return false;

// find sum of digits
int sum = sumOfDigits(number);

// find result after dividing
// by the sum of digits
int newNumber = number/sum;

// check new number is harshad number
return true;

return false;
}

public static void main(String[] args) {

// declare variables
int number = 0;
boolean result = false;

//create Scanner class object to take input
Scanner scan = new Scanner(System.in);

// take input from end-user
System.out.print("Enter an integer number:: ");
number = scan.nextInt();

// check number is Harshad number or not

// display result
if(result)
System.out.println(number +
" is a multiple Harshad number");
else
System.out.println(number +
" is not a multiple Harshad number");

// close Scanner class object
scan.close();
}
}``````

Output:-

Enter an integer number:: 21
21 is a multiple Harshad number

Enter an integer number:: 6805
6805 is not a multiple Harshad number

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!