Robotics & Artificial Intelligence

Write a program to input a number. Check and display whether it is a Niven Number or not. (A number is said to be Niven when it is divisible by the sum of its digits).

Sample Input: 126

Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.

Python Control Flow

1 Like

Answer

n = int(input("Enter a number: "))
p = n
s = 0

while n > 0:
    d = n % 10
    s = s + d
    n = n // 10

if p % s == 0:
    print(p, "is a Niven Number")
else:
    print(p, "is not a Niven Number")

Output

Enter a number: 126
126 is a Niven Number

Enter a number: 167
167 is not a Niven Number

Answered By

3 Likes


Related Questions