In this tutorial you will learn about the Program to check Harshad Number In Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Harshad Numbers in Java using Java programming.
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
Harshad number is a number that if it is divisible by the sum of its digit.
For example:=> 40, Sum of digit is (4 +0 = 4). So 40 is divisible by 4. So, 40 is a Harshad number.
Program to check Harshad Number in Java
In this program we would find given number is a Harshad number or not .first of we would take a value from user and find the Harshad number.let’s have a look at the code.
// java program to find Harsh number.
public class Harshad
public static void main(String args)
// declaring avriables and taking value form user..
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number ");
int number = sc.nextInt();
int remainder = 0, sum = 0, no;
no = number;
//Calculatings sum of digits
while(number > 0)
remainder = number%10;
sum = sum + remainder;
number = number/10;
//Checking number is divisible by sum of digits or not
if(no%sum == 0)
System.out.println(no + " is a harshad number");
System.out.println(no + " is not a harshad number");
Not a Harshad Number.
In the above program, we have first declared and initialized a set variables required in the program.
- no and number= it will hold entered number.
- remainder = it will hold remainder of values.
- sum = it will hold sum of digit
After that we take a number from user and find given number is a Harshad number or not
- First of all we take and initialize variable number.
- Duplicate the number in variable no.
- check Condition while number is greater than 0, then find remainder of number by dividing number with 10.
- Add remainder to a variable sum=>(sum = sum + remainder).
- Divide number by 10.
- Repeat the steps 3 to 5,
If no divide sum completely, then number is a Harshad number. Else, given number is not a Harshad number.