How to Test If a Number Is Divisible in Python


This example is to test if a number is divisible in Python. It uses some simple code to judge the input odd number which can be divided by 9.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

zi = int(input('Please input a number:n'))
n1 = 1
c9 = 1
m9 = 9
sum = 9
while n1 != 0:
    if sum % zi == 0:
        n1 = 0
    else:
        m9 *= 10
        sum += m9
        c9 += 1
print('%d 9s is dividable by %d: %d' % (c9,zi,sum))
r = sum / zi
print('%d / %d = %d' % (sum,zi,r))

Output:

Please input a number:
 13
6 9s is dividable by 13: 999999
999999 / 13 = 76923

Now we can see that 999999 can be divided by 13.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments