Computer Science, asked by jagamoni65, 3 months ago

Write a python function to find out whether a number is divisible by the sum of its digits. If so return True,else return False. If the number is less than or equal to 0, the function returns "Invalid Input".

Answers

Answered by XxAarzooxX
17

Explanation:

Down vote

Accepted

Well, first of all, you should put code in an if __name__ == '__main__': block to make sure it doesn't run whenever the file is imported. Thus, just copy pasting:

if __name__ == '__main__':

print 'Question 4. \n'

num = float(raw_input('Enter a number that is divisible by 2: '))

while (num%2) != 0:

num = float(raw_input('Please try again: '))

print 'Congratulations!'

The next thing to notice is that a number being even is actually a fairly common check to make. Such things should be turned into functions. Mostly copy-pasting again:

Answered by syedtahir20
0

Answer:

As per the given data,  a Python function to find out whether a number is divisible by the sum of its digits or not has been provided.

Explanation:

The code will return True or False accordingly based on the given condition,.

 If the number is to 0, the Fn. returns “Invalid Input”:

Python code:

def is_divbl_by_Tsum(n):

   if n <= 0:

       return "Invalid Input"

   else:

       sum_d = sum(int(d) for d in str(n))

       return n % sum_d == 0

The above fn. takes a single argument n, which is the no. we are required to check.

Initially, it checks if the n is <= 0, and returns "Invalid Input".

If n >0, it'll calculates the sum of its digits using a list comprehension and the built-in sum() function.

The Fn. then checks whether n is divisible by the sum of its digits using the % operator.

If the remainder is 0, it returns True, indicating that the number is divisible by the sum of its digits. Otherwise, it returns False.

For more such question: https://brainly.in/question/18675738

#SPJ2

Similar questions