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 *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close