# Multiplication Table using loops

This is a discussion on Multiplication Table using loops within the C Programming forums, part of the General Programming Boards category; Hi there, I need some help with formatting on my program. I can't seem to get the table to look ...

1. ## Multiplication Table using loops

Hi there,

I need some help with formatting on my program. I can't seem to get the table to look like this:

Code:
```
1   2   3   4
-------------------
1     1   2   3   4
2     2   4   6   8
3     3   6   9  12
4     4   8  12  16```
Here is my code:

Code:
```#include <stdio.h>

int mult(int x, int y);

int main(void)

{
int x, y;

for (x = 1; x < 10; x++)  {
printf("%4d", x);
}
printf("\n");
printf("-------------------------------------\n");

for (y = 1; y < 10; y++)  {
printf("%d\n", y);
}

for (x = 1; x < 10; x++)   {
for (y = 1; y < 10; y++)    {
printf("%4d", mult(x, y));
}
}
return (0);
}

int mult(int x, int y)
{
return(x * y);
}```
Now, I realize this would be 100x easier with arrays, but we haven't learned them in class yet, so I need to use loops. I just don't know how to get the numbers I need on the lines I want, like shown above.

Help/Tips appreciated.

2. Make your "%d" fields bigger... maybe 6 instead of 4... then put a printf("\n"); at line 32... moving the brace to 33.
Then all you need to do is adjust your header spacing at line 17 to match...

And in this case... arrays would be overkill, and present you with the same formatting problems.

3. Code:
``` for (y = 1; y < 10; y++)  {
printf("%d\n", y);
}

for (x = 1; x < 10; x++)   {
for (y = 1; y < 10; y++)    {
printf("%4d", mult(x, y));
}
}```
Well at this part of the code in your top loop you are printing a single number followed by a new line. When that's complete you are then printing your multiplication table, so you must combine them in one loop.

4. @lolcats... please don't back edit your first message. It removes the context from the discussion below it.

If you make changes, post your code again in thread order please.

Also... note that you cannot edit messages more than 1 hour old. The software here prevents it.

5. Thank you both, I was able to fix my problems and get the table set up correctly.

6. the editing was not done before any messages were made, nor was it done to the actual code. it was merely to show that I needed a table that looks like the one in the first code box, since regular text wasn't showing it properly.

7. No worries... Some forums encourage you to update the first message, we don't... I was just making sure you knew.

Popular pages Recent additions