+ 1

Hi friends! How do you create a code that checks if a number is a prime number or not in python?

3rd Jul 2020, 12:02 AM
Ikimi Daniel
Ikimi Daniel - avatar
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")
3rd Jul 2020, 12:48 AM
TiTo90
TiTo90 - avatar
+ 1
Use modulus operator to check divisibility. Use for loop to loop over the numbers smaller than the number being checked.
3rd Jul 2020, 12:33 AM
Gordon
Gordon - avatar