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
How to Make a Tkinter Label Background Transparent
How to Remove Square Brackets from List in Python
How to Count Occurrences of Key in List of Dictionaries
Python - How to Pad the Output of a MySQL Table
How to Transfer Data from One Worksheet into Another Using Python in the Same Workbook
Counting the No. of Black to White Pixels in the Image Using Opencv
How to Count the Amount of Sentences in a Paragraph in Python
Decode Utf-8 Encoding in Json String
How to Clear All Variables in the Middle of a Python Script
Python Pandas: Drop Rows of a Timeserie Based on Time Range
How to Drop Rows from Pandas Data Frame That Contains a Particular String in a Particular Column
What Is the Fastest Way to Stack Numpy Arrays in a Loop
Python Json.Loads Shows Valueerror: Extra Data
Passing a List of Values from Python to the in Clause of an SQL Query
Loading and Parsing a Json File With Multiple Json Objects
How to Convert Strings With Billion or Million Abbreviation into Integers in a List
How to Suppress Scientific Notation When Printing Float Values