+ 1
Hi friends! How do you create a code that checks if a number is a prime number or not in python?
2 odpowiedzi
+ 1
This code I take from this book: "Introducción a la programación con python", Chapter 5, 2.6, if you know spanish, I recommend, I'm studying from this book, I'm newbie, XD,
This code is to check if number is prime or not with "for-in" bucle, enjoy it XD
num = int(input("Enter number: "))
i_think_is_prime = True
for divider in range(2, num):
if num % divider == 0:
i_think_is_prime = False
break
if i_think_is_prime:
print("The number", num, "is prime")
else:
print("The number", num, "is not prime")
+ 1
Use modulus operator to check divisibility.
Use for loop to loop over the numbers smaller than the number being checked.