Print All Number Divisible by 7 and Contain 7 from 0 to 100

How to print numbers from 0 to 100 that are divisible by 3 and also 5?

You should try the modulus '%', which returns the decimal part (remainder) of the quotient.

for i in range(100): # Numbers between 0 and 100
if i % 3 == 0 and i % 5 == 0:
# If i is divisible by 3 and i is also divisible by 5 then print it
print(i)

python list comperhension, creating list of 100 numbers divided by 7

Instead of generating many numbers and keeping the good ones, what about generating only the good ones ? Multiply by 7 and you'll get only multiples of 7

list_of_num = [i * 7 for i in range(100)]
print(len(list_of_num), list_of_num ) # 100 [0, 7, 14, ..., 693]

or use the step parameter of range :

list_of_num = [i for i in range(0, 7 * 100, 7)]

If you want to start at 7 and still get a hundred

list_of_num = [i * 7 for i in range(1, 101)]
print(len(list_of_num), list_of_num ) # 100 [7, 14, ..., 693, 700]

# or
list_of_num = [i for i in range(7, 7 * 101, 7)]

How to print numbers divisible by 7

You don't have to do all that. Use the modulo operator %% and the beauty of R's vectorization.

which(1:100 %% 7 == 0)
# [1] 7 14 21 28 35 42 49 56 63 70 77 84 91 98

Or if you're playing code golf, make it even shorter ...

which(!1:100 %% 7)
# [1] 7 14 21 28 35 42 49 56 63 70 77 84 91 98

Generating first 50 positive integers, divisible by 7

Your code works like this:

n = 7;
for(i = 1; i<=50; i++) // is i less than 50? If yes go inside for
{
printf("%8d", n); // print n (the first time it's still 7)
n = n + 7; // increase n by 7
}

As you can see you print the value of n before increasing it the first time, this means that since n was initially 7 you print that value.

If you want to exclude 7 from the prints you should either move the print below n = n + 7 or make n equal to 14 instead of 7 but still increase it by 7 inside the for loop.

print all the numbers in a range that are divisible by 4 or 5, but not both

for i in range(100, 201):
if i % 4 == 0 and i % 5 == 0:
continue
if i % 4 != 0 and i % 5 != 0:
continue
print(i)

And to print 10 per line, you can do something like:

printed = 0
for i in range(100, 201):
if i % 4 == 0 and i % 5 == 0:
continue
if i % 4 != 0 and i % 5 != 0:
continue
print(i, end=" ")
if (printed := printed+1) == 10:
printed = 0
print()

How to print 100 values which are divisible by 3 and 5?

Use this

num = int(input("How many Numbers you want :"))

i=0
Count = 0
while(Count != num):
if((i%3==0) and (i%5==0)):
Count += 1
print(i)
i = i + 1

print("Count is :",Count)


Related Topics



Leave a reply



Submit