I must test whether every number from 1 come 1000 is a multiple of 3 or a lot of of 5. The method I thought I"d carry out this would certainly be to divide the number through 3, and if the result is an integer climate it would be a lot of of 3. Exact same with 5.

How execute I check whether the number is one integer?

here is my current code:

n = 0s = 0while (n



You do this utilizing the modulus operator, %

n % k == 0evaluates true if and also only if n is precise multiple that k. In primary school maths this is well-known as the remainder native a division.

You are watching: Check if number is divisible by 3

In her current approach you execute a department and the an outcome will be either

always an essence if you use integer division, oralways a float if you use floating suggest division.

It"s just the wrong way to go about testing divisibility.


You can simply usage % Modulus operator to inspect divisibility.For example: n % 2 == 0 means n is exactly divisible through 2 and also n % 2 != 0 means n is not precisely divisible by 2.



You have the right to use % operator to examine divisiblity the a given number

The code to check whether offered no. Is divisible by 3 or 5 when no. Much less than 1000 is provided below:

n=0while n
This code appears to execute what you space asking for.

for value in range(1,1000): if value % 3 == 0 or worth % 5 == 0: print(value)Or other like

for value in range(1,1000): if value % 3 == 0 or worth % 5 == 0: some_list.append(value)Or any number of things.

I had the exact same approach. Since I didn"t understand just how to usage the module(%) operator.

6 % 3 = 0*This means if you division 6 by 3 you will certainly not have actually a remainder, 3 is a element of 6.

Now you have to relate it to your offered problem.

if n % 3 == 0*This is saying, if mine number(n) is divisible by 3 leaving a 0 remainder.

See more: What Does Santa Ride In Finland, Christmas Scavenger Hunt

Add her then(print, return) statement and continue your

a = 1400a1 = 5a2 = 3b= str(a/a1)b1 = str(a/a2)c =b<(len(b)-2):len(b)>c1 =b<(len(b1)-2):len(b1)>if c == ".0": print("yeah for 5!")if c1 == ".0": print("yeah for 3!")
For little numbers n%3 == 0 will certainly be fine. Because that very big numbers ns propose to calculate the cross sum an initial and then check if the cross sum is a multiple of 3:

def is_divisible_by_3(number): if sum(map(int, str(number))) % 3 != 0: my_bool = False return my_bool
Try this ...

public course Solution { public revolution void main(String<> args) { lengthy t = 1000; long sum = 0; for(int i = 1; ns
The simplest method is to check whether a number is an integer is int(x) == x. Otherwise, what David Heffernan said.

Highly active question. Knife 10 call (not counting the association bonus) in order to answer this question. The reputation need helps defend this inquiry from spam and non-answer activity.

Not the price you're feather for? Browse various other questions tagged python python-2.7 essence modulo or asking your own question.

How have the right to i do this heat of password cover every number in the 10 time table there is no coding every number
ask the user to kind an integer and also then prints "Yes" if that integer is divisible through 3, otherwise prints "No" (Python)