ROTechnica

Python code and Algorithm to check if a number is prime number

python_computer

Do you want to write an Algorithm to check for prime numbers. This code below is just what you need. Now let’s get started.

Algorithm to check for prime numbers

Step 1: Start

Step 2: Declare variables num, i

Step 3: Initialize variables
i= 2

Step 4: Read num from user.

Step 5: Repeat the steps until i<(num/2)
5.1 If remainder of n/i equals 0
Go to step 6

else Goto step 7

Step 6: display num is not a prime number

Step 7: display num is prime number

Step 8: stop

#Below is the python program… πŸ‘‡ 

PYTHON CODE

print(“To check for prime numbers “)

num= int(input(“Enter a number: “))

if num > 1:

 for i in range(2, num):

  if num%i == 0:

   print(num, ” is not a prime number”)

   print(i, ” times “, num//i, ” is “, num) 

   break

  else:

   print(num, ” is prime number”)

else:

print(num, ” is not prime “)

HOW TO DOWNLOAD AND RUN PYTHON PROGRAMS ON YOUR DEVICES

To run python on your android phone, download Qpython from playstore. You can click the link below :πŸ‘‡ 


Also, 

Download the latest python IDE for your computer via the link below: πŸ‘‡ 

Related posts

How To Download YouTube Videos Freely

Davide

Google Search will soon be able to answer questions you haven’t asked yet

Davide

825 Active Devices now use Windows 10: It’s implications for programmers

Francis

Leave a Comment

seventeen − five =

* By using this form you agree with the storage and handling of your data by this website.