Check Prime Number

Learn how to check whether the given number is prime or not through Brute Force Approach.

Check prime number

It is easy to find if some number (say N) is prime or not. You simply need to check if at least one number from numbers lower or equal to sqrt(n) is a divisor of N.

For example, let us have n = 13. We will now iterate from 2 to sqrt(13) = 3 (we are taking integers) and check if there is any number that can divide n. If we find this condition to be true, then n is definitely not a prime number and vice-versa.

This can be achieved by a simple code shown below:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.