# Thread: Multiplication table (need guide)

1. ## Multiplication table (need guide)

hey there..
i kinda new for programmin..
i need guide from u guys to help me to solve multiplication
table program...

Question: Write a program that accepts an integer in between 1 and 20 only (1 to 20) from the
user then produces the multiplication table as shown below;
Explanation: The number that the user entered is the starting number, and it will be incremented
by 1 for three times such that the body of the multiplication table has 4 rows and
4 columns, as shown below. You should use loop to create the body of the
multiplication table

OUTPUT:

Enter a number (1 to 20): 0
Enter a number (1 to 20):22
Enter a number (1 to 20): 2

****:****2****3*****4******5 --> \*ignore ****: just wanna add tab, \t there.. *\
---------------------------------
2**:*****4****6*****8*****10
3**:*****6****9****12*****15
4**:*****8***12****16****20
5**:****10***15****20****25

2. Think about it for a while.
This program is pretty simple.
All you have to do is count 4 numbers up from the input and show the multiplication for the first elements for each one of them.
Write some code and see where you get, after that ask for help if your stuck.
Start with the easiest part which you are sure how to do, and then tackle the more advanced stuff.

3. Originally Posted by Spidey
Think about it for a while.
This program is pretty simple.
All you have to do is count 4 numbers up from the input and show the multiplication for the first elements for each one of them.
Write some code and see where you get, after that ask for help if your stuck.
Start with the easiest part which you are sure how to do, and then tackle the more advanced stuff.
alright.. i'll try the easiest part first.. =)
thanks spidey =)

4. should i insert "while" to control the user input?

5. >> should i insert "while" to control the user input?

I wouldn't worry about that part just yet. Focus on getting a program up and running with hard-coded values. Once that's running properly you can expand it to take user input.

6. Originally Posted by Sebastiani
>> should i insert "while" to control the user input?

I wouldn't worry about that part just yet. Focus on getting a program up and running with hard-coded values. Once that's running properly you can expand it to take user input.
i got stuck here.. :x

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

int main()
{
int i, j, x;
printf("Enter a number between 1 to 20 (1-20): ");
scanf("%d", &x);

for ( i = x; i <= x + 4; i++ )  {
for ( j = x; j <= x+3; j++ )
printf( "\t%d", i*(i+1) );
printf( "\t%d", j*i );
printf( "\n" );
}

return 0;
}

output:
Enter a number between 1 to 20 (1-20): 5
30      30      30      30      45
42      42      42      42      54
56      56      56      56      63
72      72      72      72      72
90      90      90      90      81```
can sumone consult me.. plz~ :sweat:

However, what is the purpose of this line -

Code:
`printf( "\t%d", i*(i+1) );`
also, your for loop is missing parentheses, so only the first line will execute and skip the rest. Hence j will only show the value which it received at the end of the for loop.

Code:
```for ( i = x; i <= x + 4; i++ )  {
for ( j = x; j <= x+3; j++ )
printf( "\t%d", i*(i+1) );
printf( "\t%d", j*i );
printf( "\n" );
}```
Think about which lines are necessary in the for loop and which ones are not, and then restructure your loop.

8. Code:
```   for ( i = x; i <= x + 3; i++ )  {
for ( j = x; j <= x + 3; j++ )
// print current product of i and j
printf( "\n" );
}```

9. Originally Posted by Spidey

also, your for loop is missing parentheses, so only the first line will execute and skip the rest. Hence j will only show the value which it received at the end of the for loop.
ok... so.. is my new for loop correct now?
Code:
```   for ( i = x; i <= x + 3; i++ )  {
for ( j = x; j <= x+3; j++ ) {
printf( "\t%d", i++);
}
printf( "\t%d", x*(j++));
printf( "\n" );
}```

10. >> ok... so.. is my new for loop correct now?

Why are you incrementing the values of 'i' and 'j' within the print statement? Also, why print the value of 'i' at all? And why multiply 'j' with 'x'? Think about it. You *just* need the product of 'i' and 'j' from within the innermost loop!

11. Originally Posted by Sebastiani
>> ok... so.. is my new for loop correct now?

Why are you incrementing the values of 'i' and 'j' within the print statement? Also, why print the value of 'i' at all? And why multiply 'j' with 'x'? Think about it. You *just* need the product of 'i' and 'j' from within the innermost loop!
for ur question: i thought i can get my multiplication table.. but, i'm wrong..
ok.. i'd change my for loop..
but the output kinda weird..

Code:
```for ( i = x; i <= x + 3; i++ )  {
for ( j = x; j <= x+3; j++ ) {
printf( "\t%d", j);
printf( "\t%d", i);
}
printf( "\n" );
}
OUTPUT
Enter a number between 1 to 20 (1-20): 5
5       5       6       5       7       5       8       5
5       6       6       6       7       6       8       6
5       7       6       7       7       7       8       7
5       8       6       8       7       8       8       8```

12. Now you are just displaying the values of i and j without multiplying anything.
Like Sebastiani said, heres what you have to do -

Originally Posted by Sebastiani
Code:
```   for ( i = x; i <= x + 3; i++ )  {
for ( j = x; j <= x + 3; j++ )
// print current product of i and j
printf( "\n" );
}```

13. >> for ur question: i thought i can get my multiplication table.. but, i'm wrong..

Ok, so now remove one print statement (either will do) and run the program. Then comment out that one and uncomment the other and run the program. Do you see how the values of 'i' and 'j' change? Now you just need to calculate the product of the two. That's all you need!

14. ok.. i try print it.. but.. the vertical line does not change..
my loop is now correct aite?
just the printf part is wrong aite?
Code:
```for ( i = x; i <= x + 3; i++ )  {
printf("\t%d", x);
for ( j = 2; j <= 5; j++ )
printf("\t%d", x*j);
printf( "\n" );

OUTPUT
Enter a number between 1 to 20 (1-20): 5
5       10      15      20      25
5       10      15      20      25
5       10      15      20      25
5       10      15      20      25```

15. Maybe this will clear things up:

Code:
```int main()
{
int i, j, x;
printf("Enter a number between 1 to 20 (1-20): ");
scanf("%d", &x);
puts("------ printing values for 'j' ------");
for ( i = x; i <= x + 3; i++ )  {
for ( j = x; j <= x+3; j++ ) {
printf( "\tj: %d", j);
//printf( "\ti: %d", i);
}
printf( "\n" );
}
puts("------ printing values for 'i' ------");
for ( i = x; i <= x + 3; i++ )  {
for ( j = x; j <= x+3; j++ ) {
//printf( "\tj: %d", j);
printf( "\ti: %d", i);
}
printf( "\n" );
}
return 0;
}```
So you see, the inner loop only needs *one* print statement. Furthermore, it just needs to print the product of the two values at that point (notice how if you multiplied the same row/column of the two output tables you'd get the value you're looking for).