Harshad Number Program in Java

Harshad Number Program in Java
Harshad Number Program in Java

In mathematics, a harshad number (or Niven number) in a given number base is an integer that is divisible by the sum of its digits when written in that base.


import java.util.Scanner;

public class HarshadNumber
{
    public static void main(String[] args)
    {
        int r, n, num,
        sum = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number=");
        n = sc.nextInt();
        num = n;
        while (num > 0)
        {
            r = num % 10;
            sum = sum + r;
            num = num / 10;
        }
        if (n % sum == 0)
        {
            System.out.println("Harshad Number");
        }
        else
        {
            System.out.println("Not Harshad Number");
        }
    }
}

Output:

Enter number=6804
Harshad Number
b. tech. bca icse java java tutorials learn java mca programs

Frequently Asked Questions

In mathematics, a harshad number (or Niven number) in a given number base is an integer that is divisible by the sum of its digits when written in that base.
In mathematics, a harshad number (or Niven number) in a given number base is an integer that is divisible by the sum of its digits when written in that base.