0

Where am I making mistake

https://code.sololearn.com/c5IiiG14Aqjm/?ref=app irrelevant results are appearing when I want to find prime for 49 81 121 169 289

5th Aug 2018, 3:09 PM
fnwttui
fnwttui - avatar
5 Respostas
+ 1
In your else block, you need to add if i==num1: [print(...)] so 13: else: 14: if i==num1: 15: print(num,” is a prime number.”) As it stands, every time you test for whether i divides your number, if it doesn’t, your code says immediately that the number is prime. Adding that if statement in means that it will onlt declare the number as prime if it has reached the end of the loop and still not found a number that divides num.
5th Aug 2018, 3:38 PM
Russ
Russ - avatar
+ 1
You could put your print statements out of the loop: import math num = int(input("Enter the number" + "\n")) print("To find that the number", num, "is prime or not") num1 = math.trunc(num**0.5) print(num1) j = 0 if num > 0: for i in range(2,num1 +1): if (num % i) == 0: j = 1 break if j == 1: print("The number", num, "is not a prime number") print( i, "times", num // i," gives the given number") else: print("The number", num, " is a prime number")
5th Aug 2018, 3:49 PM
Paul
Paul - avatar
5th Aug 2018, 4:05 PM
Louis
Louis - avatar
0
thanx understood my mistake
6th Aug 2018, 12:09 PM
fnwttui
fnwttui - avatar
0
lol I spent 1 day figuring ot
6th Aug 2018, 12:09 PM
fnwttui
fnwttui - avatar