
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How To Check Whether a Number is a Harshad Number or Not in Java?
What is Harshad Number?
A Harshad number is an integer that is completely divisible by the sum of its digits, which means that there is no remainder when the number is divided by this sum.
For example, consider the number 81. If we calculate the sum of its digits (8 + 1), which is 9. Divide the number 81 by the sum result 9, there will be no remainder, The number 81 is a Harshad number.
Here are some other examples of Harshad numbers such as 1, 2, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, etc. In this article, we will see how to check Harshad numbers by using the Java programming language.
Input & Output Scenarios
Below are a few input and output scenarios that help you understand how you can calculate and check a Harshad number mathematically and which logic you can apply in the program:
Scenario 1
Suppose the number is 18:
Input: 18 Output: Yes Calculation: sum of digits of number 18 = 1 + 8 = 9 divide number 18 by sum 9 = 18/9 = 2, reminder = 0;
Since the number 18 is completely divisible by the sum of its digits 9, the number 18 is a Harshad number.
Scenario 2
Suppose we have an input number 15:
Input: 15 Output: No Calculation: sum of digits of number 15 = 1 + 5 = 6 divide number 18 by sum 9 = 15/6 = 2, reminder = 3;
The number 15 is not completely divisible by its digits' sum 6, 15 is not a Harshad number.
Steps
- First, get each digit of the given number.
- Calculate the sum of the digits.
- Divide the given number by the sum of the digits.
- Check whether it is completely divisible or not; if divisible, display "Yes" or else "No".
Example 1
In the following program, we calculate the sum of the digits of the number 21. Then, we divide the number by this sum; if there is no remainder, the number is a Harshad number; otherwise, it is not:
public class checkHarshad{ public static void main(String args[]){ int originalNumber = 21; System.out.println("The given number is: " + originalNumber); int copyOfOriginalNumber = originalNumber; int sum = 0; while(originalNumber > 0){ //find the last digit of a number int rem = originalNumber % 10; //calculate the sum of each digit sum = sum + rem; //remove the last number on each iteration originalNumber = originalNumber / 10; } if(copyOfOriginalNumber % sum == 0){ System.out.println("Yes! " + copyOfOriginalNumber + " is a Harshad number"); } else{ System.out.println("No! " + copyOfOriginalNumber + " is not a Harshadnumber"); } } }
The above program produces the following output:
The given number is: 21 Yes! 21 is a Harshad number
Example 2
In the example below, we define the checkHarshadNumber() method, which calculates the sum of each digit of a number 56 and returns true or false based on whether the number is completely divisible by zero:
public class checkHarshad{ //define a method to check harshad number public static boolean checkHarshadNumber(int num){ int temp = num; int sum = 0; while(num > 0){ //find the last digit of a number int rem = num % 10; //calculate the sum of each digit sum = sum + rem; //remove the last number on each iteration num = num / 10; } if(temp % sum == 0){ return true; } return false; } public static void main(String args[]){ int originalNumber = 56; System.out.println("The given number is: " + originalNumber); //calling the checkHarshadNumber() method to check the Harshad number System.out.println("Is number " + originalNumber + " is a Harshad number: " + checkHarshadNumber(originalNumber)); } }
Following is the output of the above program:
The given number is: 56 Is number 56 is a Harshad number: false
Example 3
The following program converts a given number 40 into a string using the toString() method and accesses each element using the charAt() method by iterating over it.
Then, we subtract the character '0' from each character to get the actual numeric value (ASCII) of that character and calculate the sum of these values. If the original number is divisible by this sum, it is a Harshad number, else no:
public class checkHarshad{ public static void main(String args[]){ int num = 40; System.out.println("The given number is: " + num); //keep a copy of original number int copyOfOriginalNumber = num; int sum = 0; //convert the integer to string by using toString() method String str = Integer.toString(num); for(int i = 0; i < str.length(); i++){ //subtarct from 0 to check current digit sum += str.charAt(i) - '0'; } if(copyOfOriginalNumber % sum == 0){ System.out.println("Yes! " + copyOfOriginalNumber + " is a Harshad number"); } else{ System.out.println("No! " + copyOfOriginalNumber + " is not a Harshad number"); } } }
Below is the output of the above program:
The given number is: 40 Yes! 40 is a Harshad number