# Thread: multiplication tables using for statements

1. ## multiplication tables using for statements

I'm having severe difficulty creating a multiplication table. I need to create a multiplication table that looks like this by multiplying i*j

1*1= 1 1* 2=2 1* 3=3 1* 4=4 1* 5=5
2*1= 2 2* 2=4 2* 3=6 2* 4=8 2* 5=10
3*1= 3 3* 2=6 3* 3=9 3 * 4=12 3* 5=15
4*1= 4 4* 2=8 4*3=12 4* 4=16 4* 5=20
5 1= 5 5* 2=10 5*3=15 5* 4=20 5* 5=25
This is what I have. I can get a table of i*j I just need to figure out how to put a for statement that could increment itself 5 times for j and start over and then have i increment by 1.
Code:
```#include <stdio.h>
#include "conio.h"

const int num_rows = 5;
const int num_columns = 5;

int main(void)
{
int row, column, i, j;

printf("i*j\t", '*');
for (i= 1; i<=5; i++)
if (i <= 5)
break;
printf( "%d", i);

for (column = 1; column <num_columns; column++)
printf("i*j\t", column);
putchar('\n');
for (i= 1; i<=5; i++)
if (i <= 5)
break;,md

for (row =1; row <num_rows; row++)
{
printf("i*j\t", row);

for(column =1; column < num_columns; column++)
{
printf("i*j\t", row *column);
}
printf("\n");
}
_getch();
return 0;
}```
Thanks everyone.

2. Here are some warnings to think about.
Code:
```\$ gcc -std=c99 -Wall baz.c
baz.c: In function ‘main’:
baz.c:10: warning: too many arguments for format
baz.c:17: warning: too many arguments for format
baz.c:25: warning: too many arguments for format
baz.c:29: warning: too many arguments for format
baz.c:8: warning: unused variable ‘j’```
They refer to most of your printf statements, which are not doing what you think they are.

You only need the last pair of loops, if you do it right.

3. wow your code is very long, and not vary readable, here is what I would do:
Code:
```    int i;
for(i=0; i<25; i++){
printf(" %-1d*%-1d=%-2d %c", (i/5)+1, (i%5)+1, ((i/5)+1)*((i%5)+1), (i%5-4)?' ':'\n');
}```

4. It's a lot more readable than that one-line cryptic horror.

5. Code:
`const int num_rows = 5;`
it is better practice to use #define 5 N

Code:
```printf("i*j\t", column);
printf("i*j\t", row *column);```
I don't know what u expect this to do.

Code:
`break;,md`
or this

6. Originally Posted by Salem
It's a lot more readable than that one-line cryptic horror.
hhhhhhhhh cryptic horror, just wanted to show that it doesn't have to be so long.

7. And you think yours will be any less, when you replace all those literals with the symbolic names in the original?

Or what you would do, if you had to do anything else other than just print the answer (basically delete the code and start again with for loops).

8. So should I use the const int for number of rows and columns or can I do this with declaring i and j as int? I'm kinda lost on how I can do that though, I know that I should be able to use two "for" loops but I have no idea how to go about doing this. If I could program this right it would be due to serendipity because I don't really understand these for statements, especially in this application. Thanks everyone!

9. This is all you need.
Code:
```	for (row =1; row <num_rows; row++)
{
printf("i*j\t", row);

for(column =1; column < num_columns; column++)
{
printf("i*j\t", row *column);
}
printf("\n");
}```
The only thing you need to do is figure out how to fix the inner printf statement so it prints say 5*2=10 rather than just i*j all the time.

You're pretty much there, you just need to tweak the right bits into the right place.

10. This is what I have so far
Code:
```#include <stdio.h>
#include "conio.h"

const int num_rows = 5;
const int num_columns = 5;

int main(void)
{
int row, column, i,j result;

result = i * j ;
for ( i=1; row < num_rows; row++) //for (row =1; row <num_rows; row++)

{

printf("%d*%d=%d\t", i,j, result);
printf("\n");
for (j =1; j < num_columns; j++)

{
printf("%d*%d=%d\t", i, j, result);
printf("\n");
}
printf("\n");
}```
I was hoping changing the for statements and the printf statements around would fix the problem but it hasn't. It keeps telling me i and j are being used without being initialized but I can't figure out why Thanks everyone.

11. > result = i * j ;
Move this inside both for loops.
You only need one print statement printing values.

12. Yay I sort of got it to work but now the formatting is off and I can't fix it with \n.

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

int main(void)
{
int i, j;

for (i = 1; i < 6; i++)
{	printf("\t");

for (j = 1; j < 6; j++)
printf("\t");

printf("%d*%d = %d\t", i, j, (i * j));

}
}

_getch();
return 0;
}```

13. or \t

14. You are basically there. The loops are still a little overworked. Here is one possible solution:
Code:
```#include <stdio.h>
#include "conio.h"

#define NUM_ROWS 5
#define NUM_COLUMNS 5

int main(void)
{
for (int column = 1; column < NUM_COLUMNS+1; column++){
for (int row = 1; row < NUM_ROWS+1; row++){
printf("%d*%d = %d \t", row, column, (row * column));
//A space is between %d and \t
}
printf("\n");
}

_getch();
return 0;
}```