0

Can you say what is error in this program.To find prime number from 1 to 8.

for i in range(1,9): j=2 while j<i: if i%j==0: break j=j+1 print(i) Output: It's printing 3 5 5 5 7 7 7 7 7 > Expected output 1 2 3 5 7

19th Jan 2021, 4:27 PM
Ramana Prasad j
Ramana Prasad j - avatar
1 Réponse
+ 3
you can use the >else< part of the while loop. put your print statement in there: for i in range(1,9): j=2 while j<i: if i%j==0: break j=j+1 else: print(i)
19th Jan 2021, 4:41 PM
Lothar
Lothar - avatar