Write a Java Program to Check Harshad Number

In the previous article, we have have discussed Java Program to Check Happy Number

In this article we are going to understand what Harshad number is and how we can check whether a number is Harshad or not in Java with examples.

Java Program to Check Harshad Number

Harshad numbers or Riven numbers are integers in a given number that is divisible by the sum of its digits when written in that base.

Example:

2020 -> Harshad number
6804 -> Harshad number
9 -> Not a Harshad number

In the above examples the numbers 2020  and 6804 both are Harshad numbers while 9 is not.

Let’s see different ways to check whether a number is Harshad number or not.

Approach:

  1. We ask the user to enter a number which we store in an integer variable num.
  2. We then use a while loop to iterate digits in the number and calculate the sum.
  3. After coming out of loop if the resultant sum is equal to the number then the number is a Harshad number, else it is not.

Method-1:Java Program to Check Harshad Number By Using Static Value

import java.util.Scanner;
public class HarshadNumber
{
    public static void main(String args[])
    {
        //a number declared
        int num = 6804;

        //Temp is used to preserve orignal variable
        int temp = num, remainder, sum = 0;
        //Loop that iterates all digits and adds them
        while (temp > 0)
        {
            remainder = temp % 10;
            sum = sum + remainder;
            temp = temp / 10;
        }
        if (num % sum == 0)
        {
            System.out.println(num+" is a Harshad Number");
        }
        else
        {
            System.out.println(num+" is Not a Harshad Number");
        }
    }
}
Output:

6804 is a Harshad Number

Method-2:Java Program to Check Harshad Number By Using User Input Value

import java.util.Scanner;
public class HarshadNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        //Temp is used to preserve orignal variable
        int temp = num, remainder, sum = 0;
        //Loop that iterates all digits and adds them
        while (temp > 0)
        {
            remainder = temp % 10;
            sum = sum + remainder;
            temp = temp / 10;
        }
        if (num % sum == 0)
        {
            System.out.println(num+" is a Harshad Number");
        }
        else
        {
            System.out.println(num+" is Not a Harshad Number");
        }
    }
}
Output:

Enter a number : 40
40 is a Harshad Number

Method-3:Java Program to Check Harshad Number By Using User Defined Method

import java.util.Scanner;
public class HarshadNumber
{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();
        
        //calling the user defined method
        //to check Harshad number or not.
        checkNumber(num);
    }
    
    //checkNumber() METHOD TO CHECK HARSHAD NUMBER
    public static void checkNumber(int num)
    {
        //Temp is used to preserve orignal variable
        int temp = num, remainder, sum = 0;
        //Loop that iterates all digits and adds them
        while (temp > 0)
        {
            remainder = temp % 10;
            sum = sum + remainder;
            temp = temp / 10;
        }
        if (num % sum == 0)
        {
            System.out.println(num+" is a Harshad Number");
        }
        else
        {
            System.out.println(num+" is Not a Harshad Number");
        }
    }
}
Output: 

Enter a number : 40 
40 is a Harshad Number

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: