+ 3

I want to ask why is 9, 25, and 49 still prints the output? They are not prime numbers

https://code.sololearn.com/c3tkJAyRpcIQ/?ref=app

20th Jan 2018, 3:56 AM
JRez
JRez - avatar
6 Réponses
+ 4
There was some indentation errors and an extra while loop where an if was all that was required. Here is the corrected code: numb = 49 if (numb<100): numb2 = 2 while (numb2 <= (numb / numb2)): if not (numb % numb2): print(numb," is not a prime.") break numb2 += 1 if (numb2 > (numb/numb2)): print(numb, " is a prime.") break else : print("Your Number exceeded 100")
20th Jan 2018, 4:12 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Use a loop. First convert the code into a function, and then in a loop from 1 to 100, run the function for each number. The program will then print the numbers. Here is a code for the same. def prime(numb): if numb==1: return numb2 = 2 while (numb2 <= (numb / numb2)): if not (numb % numb2): break numb2 += 1 if (numb2 > (numb/numb2)): print(numb, " is a prime.") for i in range(1,100): prime(i)
20th Jan 2018, 4:45 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
thank you for the corrected code. but how i will make it to print every prime number < 100?
20th Jan 2018, 4:43 AM
JRez
JRez - avatar
+ 1
so for loop is the answer?
20th Jan 2018, 4:46 AM
JRez
JRez - avatar
+ 1
thank you very much.. i will study the code..
20th Jan 2018, 4:55 AM
JRez
JRez - avatar
20th Jan 2018, 6:20 AM
JRez
JRez - avatar