Python Forum

Full Version: how to divide array number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Array = [1,33,99,199,25]

I would like to ask how can write a code to arrange my array number can be divided by 3 and 5
can anyone help me thanks.

for example

33,99 can divide by 3
25 can divide by 5
Use the modulo operator %. This gives the remainder of a division. If the result is 0 then the number can be devided.
>>> 1 % 3
1
>>> 33 % 3
0
>>> 99 % 3
0
>>> 199 % 3
1
>>> 25 % 3
1
And after that you can do the same with 5.
(Dec-23-2019, 10:20 AM)ibreeden Wrote: [ -> ]Use the modulo operator %. This gives the remainder of a division. If the result is 0 then the number can be devided.
 >>> 1 % 3 1 >>> 33 % 3 0 >>> 99 % 3 0 >>> 199 % 3 1 >>> 25 % 3 1 
And after that you can do the same with 5.
0 mean is pass meaning and 1 fails meaning right
0 means pass and any other number means fail. (It is the remainder of the division.)
May I ask if I used for loop to single checking the number, the code can how to write.
foo = [1,33,99,199,25]
for char in foo:
if char % 3:
print('ok')
else:
print('ng')
Please format your code according to the rules. Use [python] at the start of the code block and [/python] at the end. Read BBCode how to do this.