Docsity

Python program to find if a number is PRIME or not, Exercises of Computer science

Toggle sidebar
bg1
PRIME
Aim
Write a python program to check a number whether it
is prime or
not.
Algorithm
1. Start
2. Enter a number and save it in n
3. Check whether n is divisible by the numbers from 2
to n -1
4. If divisible, display number is not prime
5. Otherwise, display prime
6. Stop
Program
n=int(input('Enter a number: '))
flag=True
for i in range(2,n):
if n==2:
break
elif n%i==0:
flag=False
break
if flag==True:
print(n,"is a Prime number")
pf2

Partial preview of the text

Download Python program to find if a number is PRIME or not and more Exercises Computer science in PDF only on Docsity!

PRIME

Aim

Write a python program to check a number whether it is prime or not.

Algorithm

  1. Start
  2. Enter a number and save it in n
  3. Check whether n is divisible by the numbers from 2 to n -
  4. If divisible, display number is not prime
  5. Otherwise, display prime
  6. Stop

Program

n=int(input('Enter a number: ')) flag=True for i in range(2,n): if n==2: break elif n%i==0: flag=False break if flag==True: print(n,"is a Prime number")

else: print(n,"is not a prime number")

Output

Enter a number: 12 12 is not a prime number Enter a number: 3 3 is a Prime number

Result

The python program to check whether a number is prime or not is successfully completed and the output is verified.