How to check whether a number is prime or not?



How to check whether a number is prime or not?..

Answer / Suresh Kumar Patel

A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself. Here's a simple PHP function for checking if a given number is prime:

```php
function is_prime($num) {
if ($num <= 1) {
return false;
}
for ($i = 2; $i < sqrt($num); $i++) {
if ($num % $i === 0) {
return false;
}
}
return true;
}
```
This function checks all numbers up to the square root of the input number (since a larger factor of the number would be smaller than its square root) and returns `true` if the number is prime, otherwise `false`.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More PHP Interview Questions

What are Sub routines?

1 Answers  


Does strlen include null?

1 Answers  


How would you impletement download and upload a file in php

2 Answers   A1 Technology,


Explain the difference between array_merge() and array_combine()?

1 Answers  


How do you destroy a session?

1 Answers  


Which functions are used to count the total number of array elements in php?

1 Answers  


What are the characteristics of php variables?

1 Answers  


What is escape data in php?

1 Answers  


Is age an interval or ratio?

1 Answers  


in PHP for pdf which library used?

1 Answers  


How to remove the new line character from the end of a text line?

1 Answers  


Tell us how can we access the data sent through the url with the post method?

1 Answers  


Categories