0
How to write these prime numbers 8 per a line?
for Number in range (1, 10001): count = 0 for i in range(2, (Number//2 + 1)): if(Number % i == 0): count = count + 1 break if (count == 0 and Number != 1): print(" %d" %Number, end = ' ') It comes 10 prime numbers per a line but i want to print 8 per a line .how can i code for this?
5 odpowiedzi
+ 4
Sorry, I chose the wrong number. 😅 Forgot to change it when copypasting.
+ 2
Does this helps?
lower = 2
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
count=0
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num,end=" ")
count+=1
if count>=8:
print("")
count=0
+ 1
This would be my modification.
count = 0
for Number in range (2, 10001):
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
break
else:
count = count + 1
print(
" %d" %Number,
end = ' ' if count%5 else '\n'
)
0
Oh that's it.
thanks 😊
i tried by this way before
0
But it will print 5 numbers per line.we can use various numbers as we want.thank you 😊