Definition:
A Prime Number is a number that has exactly two factors:
1
The number itself
Examples: 2, 3, 5, 7, 11, 13
Not Prime: 4, 6, 8, 9, 10 (because they have more than two factors).
Algorithm:
1.Start
2.Read the number n
3.Initialize i = 1 and count = 0
4.Check whether i <= n
5.If n % i == 0, increment count by 1
6.Increment i by 1
7.Repeat steps 4 to 6 until i > n
8.If count == 2, print "Prime Number"
9.Otherwise, print "Not a Prime Number"
10.Stop.
Flowchart:
┌───────┐
│ Start │
└───┬───┘
│
▼
┌─────────────┐
│ Input n │
└─────┬───────┘
│
▼
┌─────────────┐
│ i = 1 │
│ count = 0 │
└─────┬───────┘
│
▼
┌───────┐
│i <= n?│
└──┬─┬──┘
Yes No
│ │
▼ ▼
┌─────────┐
│count=2 ?│
└──┬───┬──┘
Yes No
│ │
▼ ▼
┌────────┐ ┌────────────┐
│ Print │ │ Print │
│ Prime │ │ Not Prime │
└────┬───┘ └─────┬──────┘
│ │
▼ ▼
┌───────┐
│ Stop │
└───────┘
(If i <= n)
▼
┌─────────┐
│n % i=0 ?│
└──┬───┬──┘
Yes No
│ │
▼ │
┌───────────┐
│count+=1 │
└─────┬─────┘
│
▼
┌─────────┐
│ i=i+1 │
└────┬────┘
│
└──────► Back to i <= n
code:
n = int(input("Enter a number: "))
i = 1
count = 0
while i <= n:
if n % i == 0:
count += 1
i += 1
if count == 2:
print("Prime Number")
else:
print("Not a Prime Number")
Output 1
Input:
Enter a number: 7
Output:
Prime Number
Output 2
Input:
Enter a number: 8
Output:
Not a Prime Number
Top comments (0)