# Thread: [C programming] multiplication table

1. ## [C programming] multiplication table

I am currently taking a C programming class and I am having a hard time with a certain program. The program is supposed to create a multiplication table that has the column headings tabbed over and the row headings on the left going down. Well, I have created a program to do that, but the paper I says that I need to use nesting loops for the rows instead of a loop for each row. How can I accomplish this?

This is source code I have typed up and should give an idea of what the multiplication table should look like.

Code:
```/*
* Author:  B.C.
* Date:  9/7/09
* Version: 0.0.1
* Class: CIT 131 / Fall 2009
* Assignment: Create a program that prints out a multiplication table.
*/

#include <stdio.h> /* import standard input/output library */B

int main()
{
int num = 10; /* declare variable and set it to ten for the column headings */
int num2 = 10; /* declare variable for multiplying the numbers */
int product; /* declare variable to hold the products of each multplication, before it is displayed */
int num3 = 10;

for (num; num >= 1; num--) /* use a for loop to create column headings */
{
printf("\t%d", num); /* display current value of the num variable */
}

printf("\n"); /* insert newline, in order to create row headings */

/* the following loops and their nested loops display a row heading and displays the products of the row heading and the corresponding column heading */
for (num3; num3 >= 10; num3--)
{
printf("%d", num3);

for (num2; num2 >= 10; num2--) /* display one product */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten for the next row */

}

num3 = 9; /* ensure num3 is set to nine */

for (num3; num3 >= 9; num3--)
{
printf("%d", num3);

for (num2; num2 >= 9; num2--) /* display two products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten */

}

num3 = 8; /* ensure num3 is set to eight */

for (num3; num3 >= 8; num3--)
{
printf("%d", num3);

for (num2; num2 >= 8; num2--) /* display three products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten */

}

num3 = 7; /* ensure three is set to seven */

for (num3; num3 >= 7; num3--)
{
printf("%d", num3);

for (num2; num2 >= 7; num2--) /* display four products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten */

}

num3 = 6; /* ensure num3 is set to 6 */

for (num3; num3 >= 6; num3--)
{
printf("%d", num3);

for (num2; num2 >= 6; num2--) /* display five products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten */

}

num3 = 5; /* ensure num3 is set to five */

for (num3; num3 >= 5; num3--)
{
printf("%d", num3);

for (num2; num2 >= 5; num2--) /* display six products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten */

}

num3 = 4; /* ensure num3 is set to four */

for (num3; num3 >= 4; num3--)
{
printf("%d", num3);

for (num2; num2 >= 4; num2--) /* display seven products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten */

}

num3 = 3; /* ensure num3 is set to three */

for (num3; num3 >= 3; num3--)
{
printf("%d", num3);

for (num2; num2 >= 3; num2--) /* display 8 products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten */

}

num3 = 2; /* ensure num3 is set to two */

for (num3; num3 >= 2; num3--)
{
printf("%d", num3);

for (num2; num2 >= 2; num2--) /* display nine products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

num2 = 10; /* reset num2 back to ten */

}

num3 = 1; /* ensure num3 is set to 1 */

for (num3; num3 >= 1; num3--)
{
printf("%d", num3);

for (num2; num2 >= 1; num2--) /* display ten products */
{
product = num2 * num3;
printf("\t%d", product);
}
printf("\n");

}

return 0;
}```

2. ## Re: [C programming] multiplication table

I'm not really understanding your method of doing this. From what I can tell, the outer for loops are only iterating once, and so are essentially useless.

I'm pretty sure all you need is something like this.

Code:
```for(int i = 0; i <= X_MAX; i++){
for(int j = 0; j <= Y_MAX; j++){
// Calculations here
printf("%d ", calculated_num);
}
printf("\n");
}```
I dunno something like that would probably work.
Last edited by Finalfantasykid; September 7th, 2009 at 11:08 PM.

3. ## Re: [C programming] multiplication table

Originally Posted by Finalfantasykid
I'm not really understanding your method of doing this. From what I can tell, the outer for loops are only iterating once, and so are essentially useless.

I'm pretty sure all you need is something like this.

Code:
```for(int i = 0; i <= X_MAX; i++){
for(int j; j <= Y_MAX; j++){
// Calculations here
printf("%d ", calculated_num);
}
printf("\n");
}```
I dunno something like that would probably work.
You would probably want to initialize the index i to a 1 (not a zero), and also initialize the index j to a 1.

4. ## Re: [C programming] multiplication table

Woops, I forgot to initialize j >.<

*fixed*

And I initialized to 0 because I felt like it Why shouldn't 0 count lol

5. ## Re: [C programming] multiplication table

Originally Posted by Finalfantasykid
I'm not really understanding your method of doing this. From what I can tell, the outer for loops are only iterating once, and so are essentially useless.

I'm pretty sure all you need is something like this.

Code:
```for(int i = 0; i <= X_MAX; i++){
for(int j = 0; j <= Y_MAX; j++){
// Calculations here
printf("%d ", calculated_num);
}
printf("\n");
}```
I dunno something like that would probably work.
I'm going to try this one right now. Also, for some reason gcc does not like me to declare variables as integers inside for loops, but Java never complains about it.

6. ## Re: [C programming] multiplication table

Originally Posted by gotenks05
I'm going to try this one right now. Also, for some reason gcc does not like me to declare variables as integers inside for loops, but Java never complains about it.
Maybe because both are completely different things?

Anyway, C89 didn't allow that, I think that C99 does. Compile with -std=c99.

Edit: Looking at the code, I'm impressed when people find themselves copy+pasting code and changing some variables, never stopping to think "maybe there's a better way to do this".
Last edited by Can+~; September 8th, 2009 at 03:27 AM.

7. ## Re: [C programming] multiplication table

Originally Posted by Can+~
Maybe because both are completely different things?

Anyway, C89 didn't allow that, I think that C99 does. Compile with -std=c99.

Edit: Looking at the code, I'm impressed when people find themselves copy+pasting code and changing some variables, never stopping to think "maybe there's a better way to do this".
Actually Java is based on C and looking at the code somebody typed in, they typed the for loop just like a person would in Java.

GCC brings up this message, if I declare variables as integers in for loops

error: ‘for’ loop initial declaration used outside C99 mode
I'm just surprised something like this was assigned, when the chapter does not even discuss something like this, only single loops are discussed and the info going all in one direction instead of two or three different directions.
Last edited by gotenks05; September 8th, 2009 at 03:44 AM.

8. ## Re: [C programming] multiplication table

Hmm, thats weird, I thought thats how I did it when I took c, but I am obviously wrong. I just compiled it now, and I got the same error. I must be writting too much Java or something ;P.

Code:
```int i;
for(i = 0; i <= Y_MAX; i++){
int j;
for(j = 0; j <= X_MAX; j++){
int calculated_num = i*j;
printf("%5d", calculated_num);
}
printf("\n");
}```
This is a better version of my above code, which has better formatting, and I noticed that X_MAX and Y_MAX were in the wrong for loops. And this one actually compiles
Last edited by Finalfantasykid; September 8th, 2009 at 03:55 AM.

9. ## Re: [C programming] multiplication table

Originally Posted by gotenks05
Actually Java is based on C and looking at the code somebody typed in, they typed the for loop just like a person would in Java.
Java was inspired on C. They kept the syntax for the sake of popularity (new syntax = old programmers won't like it), but the fundamental underpinnings of Java are different in every way.

They are both statically typed, but C is unsafe, doesn't have Object "Orientedness" (builtin) and doesn't deal with memory directly (Garbage Collector, pointers are hidden).

But w/e, I got your point.

Originally Posted by gotenks05
GCC brings up this message, if I declare variables as integers in for loops
Originally Posted by Finalfantasykid
Hmm, thats weird, I thought thats how I did it when I took c, but I am obviously wrong. I just compiled it now, and I got the same error. I must be writting too much Java or something ;P.
Again,

Code:
`~\$gcc -std=c99 mycode.c -o myexecutable`

10. ## Re: [C programming] multiplication table

To make things easier, I decided to use only one for loop. for the top row (aka. column headings) and used while loops for the rows, but it only displays products for the first row.

The following is my updated code
Code:
```/*
* Author:  B.C.
* Date:  9/7/09
* Version: 0.0.1
* Class: CIT 131 / Fall 2009
* Assignment: Create a program that prints out a multiplication table.
*/

#include <stdio.h> /* import standard input/output library */

int main()
{
int num = 10; /* declare variable and set it to ten for the column headings */
int num2 = 10; /* declare variable for the row headings */
int num3 = 1;  /* set number of rows and columns */
int num4 = 10; /* set variable for product */

for (num2; num2 >= num3; num2--) /* this loop creates the column headings */
{

printf("\t%d", num2);
}

printf("\n"); /* create a new line */

while (num >= num3) /* This loop labels the rows and displays the product of row * column */
{

printf("%d", num);

while (num4 >= num3)
{

int product = num4 * num;

printf("\t%d", product);
num4--;
}
printf("\n");
num--;
}

return 0;
}```
This is the reason why I used the code in the first post. This would probably be much easier with arrays, but I have only learned arrays in Java, not C.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•