+ 5

How to find a number is prime or composite

in PHP

1st Feb 2018, 6:17 PM
Arpit Bhalla
Arpit Bhalla - avatar
4 Answers
+ 14
/* isPrime(p){ if (p<=1) return false; for (a = 2; a <= p/2; a++) { if (p % a == 0){ return false; break; } } return true; } */ if (p!=1&&!isPrime (p)) System.out.print(composite);
2nd Feb 2018, 8:36 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
thanks@toni
2nd Feb 2018, 9:54 AM
Arpit Bhalla
Arpit Bhalla - avatar
+ 1
For prime numbers function isPrime($p){ for ($a = 2; $a <= $p/2; $a++) { if ($p % $a == 0){ return false; } } return true; }
1st Feb 2018, 7:34 PM
Toni Isotalo
Toni Isotalo - avatar
28th Dec 2021, 6:23 PM
Akil šŸ‡§šŸ‡©
Akil šŸ‡§šŸ‡© - avatar