Simple algorithm to check if given number is a Prime number – divisible only by one and itself.

Step By Step Algorithm:

IN: Positive integer number.
OUT: true - number is prime, false - number is not prime
1. Initialize variable counter to 2 and flag isPrime to true.
2. Check if number is divisible by counter (number mod counter is equal to 0). If it is change flag isPrime to false and go to step 5.
3. Add one to counter.
4. Check if counter is smaller or equal than square root of number. If it is go back to step 2. 
5. Return flag isPrime.

Sample Input:

IN:
48
19
OUT:
false
true

 

Check If Number Is Prime Algorithm
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *