# Multiplication Table For Loop "x"

Page 1 of 1

## 5 Replies - 3852 Views - Last Post: 11 July 2013 - 09:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=324565&amp;s=d7b0e2edc130dc9fc2c6312789b0869f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Hypernal

Reputation: -1
• Posts: 125
• Joined: 29-May 12

# Multiplication Table For Loop "x"

Posted 09 July 2013 - 08:51 PM

I'm making a multiplication table using PHP. I came up with this nested for-loop:
```<?php
echo "<table border=2 width=30%";
for (\$i = 1; \$i <= 10; \$i++ ) {
echo "<tr>";
echo "<td>".\$i."</td>";
for ( \$j = 2; \$j <= 10; \$j++ ) {
echo "<td>".\$i * \$j."</td>";
}
echo "</tr>";
}
echo "</table>";
?>

```

But I realized that it lacks "x" in the upper left corner:
x 1 2 3 4
1
2
3
4

How can I do it? Thanks!

Is This A Good Question/Topic? 0

## Replies To: Multiplication Table For Loop "x"

### #2 salazar

Reputation: 87
• Posts: 539
• Joined: 26-June 13

## Re: Multiplication Table For Loop "x"

Posted 09 July 2013 - 09:35 PM

I'm not sure this is the best way. But if you want to do it efficiently try using a multi-dimensional array.
```<?php
echo "<table border=2 width=30%>";
//this will print the top part
echo "<tr>"
echo "<td> x </td>";
for (\$i = 1; \$i <= 10; \$i++) {
echo "<td>" . \$i . "</td>";
}
echo "</tr>";

for (\$i = 1; \$i <= 10; \$i++ ) {

echo "<tr>";
echo "<td>".\$i."</td>";
for ( \$j = 2; \$j <= 10; \$j++ ) {
echo "<td>".\$i * \$j."</td>";
}
echo "</tr>";
}
echo "</table>";
?>

```

### #3 Dormilich

• 痛覚残留

Reputation: 3742
• Posts: 11,320
• Joined: 08-June 10

## Re: Multiplication Table For Loop "x"

Posted 09 July 2013 - 10:06 PM

Hypernal, on 10 July 2013 - 05:51 AM, said:

How can I do it?

### #4 Hypernal

Reputation: -1
• Posts: 125
• Joined: 29-May 12

## Re: Multiplication Table For Loop "x"

Posted 09 July 2013 - 10:07 PM

salazar, on 09 July 2013 - 09:35 PM, said:

I'm not sure this is the best way. But if you want to do it efficiently try using a multi-dimensional array.
```<?php
echo "<table border=2 width=30%>";
//this will print the top part
echo "<tr>"
echo "<td> x </td>";
for (\$i = 1; \$i <= 10; \$i++) {
echo "<td>" . \$i . "</td>";
}
echo "</tr>";

for (\$i = 1; \$i <= 10; \$i++ ) {

echo "<tr>";
echo "<td>".\$i."</td>";
for ( \$j = 2; \$j <= 10; \$j++ ) {
echo "<td>".\$i * \$j."</td>";
}
echo "</tr>";
}
echo "</table>";
?>

```

But the inside values are messed up, it's not multiplication table anymore :|

### #5 salazar

Reputation: 87
• Posts: 539
• Joined: 26-June 13

## Re: Multiplication Table For Loop "x"

Posted 10 July 2013 - 02:37 PM

How are the inside values messed up? I didn't change your code. I just added code to make the top axis (x-axis).

This should be the result.

x 1 2 3 4 5 6 7 8 9 10

Aren't you just printing the bottom part with your code?

### #6 kim_barcelona555

Reputation: 2
• Posts: 54
• Joined: 01-July 13

## Re: Multiplication Table For Loop "x"

Posted 11 July 2013 - 09:05 PM

I have some modification to your code. Hope this helps
```<?php

echo "<table border=2 width=30%>";

for (\$i = 1; \$i <= 11; \$i++ ) {
\$k=1;
echo "<tr>";
if(\$i==1)
echo "<td>x</td>";
else{
\$k=\$i-1;
echo "<td>\$k</td>";
}

echo "<td>".\$k ."</td>";

for ( \$j = 2; \$j <= 10; \$j++ ) {

echo "<td>".\$k * \$j."</td>";

}

echo "</tr>";

}

echo "</table>";

?>

```