The below is the code for executing prime numbers. Plzz explain how it is working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The below is the code for executing prime numbers. Plzz explain how it is working

for($x=2;$x<=100;$x++) { for($y=2;$y<=$x;$y++) { if($x%$y==0) break; } if($x==$y) echo $x."<br>"; }

1st Nov 2018, 7:12 PM
Syed Sibteali
Syed Sibteali - avatar
1 Answer
+ 3
x is the number you want to check if it's prime or not. Now if it is divisable by some number y smaller then x (i.e. modulus is zero, i.e.divisable without rest), then it is not a prime and you can break the loop. If it is only divisable by itself, i.e. x==y, then it echos it as a prime number.
1st Nov 2018, 7:26 PM
Matthias
Matthias - avatar