Program to check if all digits of a number divides it using Python

Introduction

The task is to check whether all the digits of number N divides it.

Program to check if all digits of a number divides it using Python

Program

def check(ip_num, i) :


    return (i != 0 and ip_num % i == 0)


def checkInput(ip_num) :
    
    val = ip_num
    while (val > 0) :


        i = val % 10
        if ((check(ip_num, i)) == False) :
            return False


        val = val // 10
    
    return True


ip_num = int(input("Enter the number: "))


if (checkInput(ip_num)) :
    print("The number is divisible by its all digits!")
else :
    print("The number is not divisible by its all digits!" )

Output

Program to check if all digits of a number divides it using Python Output

Explanation

In the above python code, we iterate over all the digits of given number and check D!=0 and 124%D == 0 where D = {1, 2, 4}.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.