Prime Number Check in Python

Did you know?

A prime number is a number that can be divided evenly only by 1, or itself. A prime number must be a whole number greater than 1.

3, 5, 7, 11, 13, 17… are all examples of prime numbers.

Your challenge:

Write a program that prompts the user to enter a number between 1 and 9999. The program will inform the end-user whether their number is a prime number or not.

Hint:

In Python you can easily calculate the remainder of a division using the % sign.

For instance 7 % 2 would return 1.

So to check if a number “a” can be divided evenly by another number “b” you can check if the remainder of a/b is null using if a%b==0

Solution

So let’s look at the following code used to check if a number is a prime number or not.

Extended Challenges

  1. Tweak this code to display all the prime numbers between 1 and 9999.
  2. Write another program that takes a user input and find out if the number being entered is either odd or even.

FYI…

This challenge is inspired from one of the challenges from the “101 Computing Challenges” book.

Share Button
Posted in Computer Science, Python - Advanced, Python Challenges

Our Latest Book