Python program to check Harshad (Or Niven) number


What is Harshad/Niven number?

A Harshad number in a given number base is an integer that is divisible by the sum of its digits when written in that base, is also called Niven number .


For Example:

The number 21 is a harshad number in base 10, because the sum of the digits 2 and 1 is 3 (2 + 1 = 3), and 21 is divisible by 3.


Python Code:

  • Python program to check whether a number is Harshad/Niven number or not.

num = int(input("Enter the number to be check: "))
sum = 0
temp = num
while temp > 0:
    sum += temp % 10
    temp = int(temp/10)
x = num % sum
if x == 0:
    print(num,"is a Niven number")
else:
    print(num,"is not a Niven number")

Output:

Enter the number to be check: 21

21 is a Niven number

Python code:

  • Python program to print all Harshad/Niven number between 1 and 200.

for num in range(1,200):
    sum = 0
    temp = num
    while temp > 0:
        sum += temp % 10
        temp = int(temp/10)
    x = num % sum
    if x == 0:
        print(num,end=", ")

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80, 81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152, 153, 156, 162, 171, 180, 190, 192, 195, 198,


3 views0 comments

Recent Posts

See All

09749885548

26, Buddhadeb Bose Road, Ashrampara, Siliguri

©2020 by Filamenting. Proudly created with Wix.com