Write A Python Function To Find Out Whether A Number Is Divisible By The Sum Of Its Digits

Hi in this article let's learn how to check if a number can be divisible by the sum of its digits. You have to perform this using the Python function  by passing the original number import argument to a function.

Let's use the modulus addition and division MAD method.

 

 

Write A Python Function To Find Out Whether A Number Is Divisible By The Sum Of Its Digits

 


Write A Python Function To Find Out Whether A Number Is Divisible By The Sum Of Its Digits

Let's define a function named check_divisible and pass the number as an input argument to the Python function,  copy the original number into temp and take another variable  named as total to store the sum of all the digits of the number.

def check_divisible(number):
   temp = number
   total = 0




use the value until all the digits of the number are iterated,  take the last digit of the number by modulating it with 10,  add the last digit to the total,  and then perform the true division by 10 to remove the last digit from the temp number.

   while temp != 0:
       last_digit = temp % 10
       total += last_digit
       temp = temp // 10



When the while loop is completed the total will store the sum of all the digits of the original number.  using the if statement try modulating the number by total and check if reminder is zero. Using the if statement , print that number is completely divisible by the sum of its digits.  else print not divisible.


   if number % total == 0:
       print("Number is Divisible by Sum of its Digits")
   else:
       print("Number is Not Divisible by Sum of its Digits")






In the main program, take a variable named “num” and ask a user to enter a number and then convert it into an integer using type conversion.  after getting an input integer try to call the function by passing the number to it as a input argument

num = int(input("Enter a Number : "))
check_divisible(num)



Code
===============

def check_divisible(number):
    temp = number
    total = 0

    while temp != 0:
        last_digit = temp % 10
        total = total + last_digit
        temp = temp // 10

    if number % total == 0:
        print("Number Is Divisible By The Sum Of Its Digits")
    else:
        print("Number Is Not Divisible By The Sum Of Its Digits")


num = int(input("Enter a Number : "))
check_divisible(num)





Output
=============
Enter a Number : 23
Number Is Not Divisible By The Sum Of Its Digits



Output
===============
Enter a Number : 45
Number Is Divisible By The Sum Of Its Digits



Output
===============
Enter a Number : 56
Number Is Not Divisible By The Sum Of Its Digits



Output
============
Enter a Number : 99
Number Is Not Divisible By The Sum Of Its Digits




Conclusion
==============
Execute the above Python program by providing the random integer values to it, note down the results.


Comment down below if you have any suggestions to improve the above Python program





Post a Comment

0 Comments