niven numbers are positive integers greater than 0 that are divisoble by the sum of theor digits for example 42 is a niven number it is divisible by 4+2=6 you are given a fumnction int checknivennumber(int n); 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 dividible by the sum of its digits i.e is the quotient else return 0 assume n >0 input 36 output 4 input is 57 output is?
Answers
Answered by
26
Answer:
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