+1 vote
in Class 12 by kratos

I was asked a question in my practical to check if a number is prime or not.

Can anyone help me? Thanks in advance

1 Answer

+6 votes
by kratos
 
Best answer

This code will help you check if a number is prime or not:

<?php

function IsPrime($n) {

for($x=2; $x<$n; $x++) {  
  if($n % $x ==0) { return false;}  
}

return true;
}
$a = IsPrime(13);

echo $a ? 'This is a Prime Number.' : 'This is not a Prime Number.';

?>

...