包含100个数字的表格,仅显示是否可以除以3或5
问题描述:
Hello i have have a table in php that counts to 100 but i have to make it that only the numbers that can be divided by 3 or 5 are shown in the table
Here is my code:
<?php
echo "<table border='1'>";
for ($y=0 ; $y<10 ; $y++) {
echo "<tr>";
for ($x=1 ; $x <=10; $x++) {
echo "<td>".($y*10 + $x)."</td>";
}
echo "</tr>";
}
echo "</table>"
?>
答
Try This. if(($y % 3 == 0) || ($y % 5 == 0) )
will check whether given number is divisible by 3 or 5. If yes, then print value.
<?php
echo "<table border='1'>";
echo "<tr>";
for ($y=1 ; $y<=100 ; $y++) {
if(($y % 3 == 0) || ($y % 5 == 0) ) {
echo "<td>".$y."</td>";
}
}
echo "</tr>";
echo "</table>"
?>
Output: 3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51 54 55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100
For more info, please click PHP - If number is divisible by 3 and 5 then echo
答
to get numbers that can be divided by 3 or 5 you need to calculate it by using modular operation as follows:
for ($y=1 ; $y<100 ; $y++) {
if (($y%3 == 0) || ($y%5 == 0)) {
echo $y."
";
}
}