In this example we will learn about prime numbers and also how to check whether a number is prime or not. What is the prime number? Prime Number is a special number which is greater than 1 and has only two factors. These numbers can only be divided by 1 and themselves. For example : 3,5,7,11,13...etc..
How to check whether a number is prime or not? Program for checking a number is prime or not.
$number =4; // assign a number to a variable. like 3, 5,7,10 $flag = 0; //assign a variable into 0 for ($i = 2; $i <= $number / 2; ++$i) { // condition for non-prime if ($number % $i == 0) { $flag = 1; break; } } if ($number == 1) { print_r("1 is neither prime nor composite."); } else { if ($flag == 0) print_r("$number is a prime number."); else print_r("$number is not a prime number."); } ?>