+ 1

Find out if the number is divided by another

I created a variable that asks for a number. How to find out if this number is divisible by 10, and send "The number is divisible by 10"?

2nd Feb 2019, 9:47 AM
Ivan Morda
6 Réponses
+ 3
use % ("mod" operator), it returns remainder after division. if num is divisible by 10, remainder after division will be 0. if num%10 == 0: #divisible else : #not divisible
2nd Feb 2019, 9:56 AM
code learner
code learner - avatar
+ 5
def is_divisible_by(your_number): return your_number % 10 == 0
2nd Feb 2019, 10:31 AM
Dan Rhamba
Dan Rhamba - avatar
+ 2
Code learner Oh. I tried to do so, and an error appeared, apparently due to gaps. Thank))
2nd Feb 2019, 10:04 AM
Ivan Morda
+ 1
A different way to everyone else :) if x[-1] == '0': print('It is divisible by 10.') It checks if the last digit is 0, which is what is true with all numbers divisible by 10.
2nd Feb 2019, 9:59 AM
Eragon1980
Eragon1980 - avatar
+ 1
Dan Rhamba, I like yours :)
2nd Feb 2019, 10:39 AM
Eragon1980
Eragon1980 - avatar
+ 1
if number%10 ==0: print("the number is divisble") else: print("the number is not divisible")
3rd Feb 2019, 6:22 AM
R. Hariharann
R. Hariharann - avatar