Harshad Number or Niven Number



Problem to find the Harshad Number or not in given number.


If given number is completely divisible by sum of the each and every digit value , So It's Harshad Number or Niven Number.



Example-1:


Input    : n = 2020
Output   : Harshad Number
Explain  : 2+0+2+0 = 4. The number 2020 is completely divisible by 4.
           So it is "Harshad Number".


Example-2:


Input    : n = 17
Output   : Not a Harshad Number 
Explain  : 1+7 = 8. The number 17 is not divisible by 8.   
           So it is "Not a Harshad Number".







Solution




public class Main
{
    public static void main(String [] args)
    {   
        int num = 2020;

        int sum = 0, t = num;

        while(t!=0)
        {
            sum = sum + (t%10);

            t = t/10;
        }

        if(num % sum == 0)
        {
            System.out.print("Harshad Number");
        }

        else
        {
            System.out.print("Not a Harshad Number");
        }
    }
}
n = 2020

k = 0

for x in str(n):

    k += int(x)

if(n % k == 0):

    print("Harshad number")

else:

    print("Not a Harshad number")



Output



Harshad number