This
Niven numbers are positive integers greater than 0
that are divisible by the sum of their digits. For
example, 42 is a Niven number, it is divisible by 4 + 2
= 6. You are given a function,
def CheckNivenNumber(n)
Your
welc
"Sav
judg
Addi
exec
You
The function accepts an integer 'n' as its argument.
Implement the function to check if 'n' is Niven. If 'n'
Niven, then return the number of times it is divisible by
the sum of its digits (i.e. the quotient), else return 0.
Assume that 'n' > 0.
Answers
Answered by
0
In Python:
def func(num):
numlist = list(str(num))
result = 0
for i in numlist:
result += int(i)
if (num % result == 0):
return num/result
else:
return 0
Explanation:
First, the given number is converted into iterable form, which is LIST.
It was converted into STRING because, INTEGERs are not iterable.
Later, a variable "result" is declared and assigned a temporary value of 0.
And then, the every item in that list is added to the "result" using "for" loop.
And at last, the sum, in the result is checked whether it is divisible by the given number. If divisible, returns the quotient. Else returns 0.
Similar questions