Home » ALGORITHM » ALGORITHM AND FLOWCHART FOR PRIME NUMBER

# ALGORITHM AND FLOWCHART FOR PRIME NUMBER

Every Prime number has two factors and these are 1 and the number itself. If N is Prime Number then its factors will be 1 and N. Thus a Prime Number is divisible by 1 and itself. Example of Prime Numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 and so on. The smallest Prime Number is 2 and it is an Even Number. Except 2 all Prime Numbers are Odd Numbers.

## ALGORITHM TO CHECK PRIME NUMBER

(Check Prime Number) Let N be a Positive Integer. The Number N will be a Prime Number if it is only divisible by 1 and N. If N is divisible by one or more numbers other than 1 and N then it will not be Prime Number. Since a number is always divisible by 1 and itself. Thus in this algorithm N is divided by 2 then 3 then 4 till N – 1. If N is divisible by any number form 2 to N – 1 then is not a Prime Number. Otherwise it is a Prime Number.

Step 1: Start

Step 2: [ Take Input ] Read: N

Step 3: [ Initializing Counter and Boolean Variable ] Set: I = 2 and Flag = 0

Step 4: Repeat this step While I < N

Check If N%I == 0 Then

Set: Flag = 1 and Break the Loop

[ End of If Structure ]

[ Incrementing Counter ] Compute: I++

[ End of While Loop ]

Step 5: Check If Flag == 1 Then

Print: N is not a Prime Number.

Else

Print: N is a Prime Number.

[ End of If Else Structure ]

Step 6: Exit

## ALGORITHM TO GENERATE PRIME NUMBERS

(Generate Prime Numbers) Since the smallest Prime number is 2 therefore the Lower Limit (LL) of the range will start from 2. And the Upper Limit (UL) of the range will be given by the user. Since 2 is already a Prime number therefore the series will start from 2. If the value LL is not a Prime number then it will not be printed. Thus value stored in LL will be increased by 1 after end of each loop. The above process will continue untill value of LL is not equal to value of UL.

Step 1: Start

Step 2: [ Take Input For Range ] Read: UL

Step 3: Set: LL = 2

Step 4: Repeat While while LL ≠ UL

Step 4a: Set: N = LL, Flag = 0 and I = 2

Step 4b: Repeat While I < N

Check If N%I == 0 Then

Set: Flag = 1 and Break the Inner while Loop

[ End of If structure ]

Compute: I++ [ Incrementing Counter ]

[ End of Inner While Loop ]

Step 4c: Ckeck If Flag == 0 Then

Print: N

[ End of If structure ]

Step 4d: Compute: LL = LL + 1 [ Incrementing Lower Range ]

[ End of Outer While Loop ]

Step 5: Exit

### Related Posts

• ALGORITHM AND FLOWCHART FOR COMPOSITE NUMBER Those numbers which are divisible by other than itself and 1 are known as composite numbers. The number 4 can is divisible by 1, 2 and 4 therefore 4 is a composite number. But 7 is […] Posted in ALGORITHM
• ALGORITHM TO CHECK EVEN AND ODD NUMBERS A number is said to be even number if it leaves no remainder when divided by 2. There is an alternative definition of even number and it is as a number having any number from 0, 2, 4, 6 […] Posted in ALGORITHM
• ALGORITHM AND FLOWCHART FOR COPRIME NUMBER A Coprime number comes in pair of integers. Thus they are defined as those paired numbers which can be divided by the common number 1 are called coprime numbers. Suppose a and b are two […] Posted in ALGORITHM
• PERFECT NUMBER ALGORITHM AND FLOWCHART A number is said to be a perfect number if the sum of factors of the number (excluding number itself as a factor) is equal to the number itself. Example of perfect numbers are 6, 28, 496, […] Posted in ALGORITHM