Reputation: 81
I need to print out a multiplication table that looks like this in C:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 9 12 15 18 21 24 27 30
4 16 20 24 28 32 36 40
5 25 30 35 40 45 50
6 36 42 48 54 60
7 49 56 63 70
8 64 72 80
9 81 90
10 100
My loop to print the numbers in the correct format is a bit tedious right now:
printf(" 1 2 3 4 5 6 7 8 9 10\n");
for(i=1; i<=10; i++)
{
printf("%4d", i);
for (j=i; j<=10; j++)
{
result = i*j;
if (i == 2 && j == 2)
{
printf("%8d", result);
}
else if (i == 3 && j == 3)
{
printf("%12d", result);
}
else if (i == 4 && j == 4)
{
printf("%16d", result);
}
else if (i == 5 && j == 5)
{
printf("%20d", result);
}
else if (i == 6 && j == 6)
{
printf("%24d", result);
}
else if (i == 7 && j == 7)
{
printf("%28d", result);
}
else if (i == 8 && j == 8)
{
printf("%32d", result);
}
else if (i == 9 && j == 9)
{
printf("%36d", result);
}
else if (i == 10 && j == 10)
{
printf("%40d", result);
}
else
{
printf("%4d", result);
}
}
printf("\n");
}
I was thinking there has to be a way to make this easier, to somehow concat an int variable into the precision of the number, like this:
if (i == j)
{
printf("%(4 * i)d", result);
}
else
{
printf("%4d", result);
}
This code obviously won't work, but is there a way I can achieve something like this so I can avoid all the if/else statements in my current loop?
Upvotes: 2
Views: 314
Reputation: 753535
Here is code that implements almost what you have as the desired output:
#include <stdio.h>
int main(void)
{
printf("%4s", " ");
for (int i = 1; i <= 10; i++)
printf("%4d", i);
putchar('\n');
for (int i = 1; i <= 10; i++)
{
printf("%-*d", 4 * i, i);
for (int j = i; j <= 10; j++)
printf("%4d", i * j);
putchar('\n');
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 9 12 15 18 21 24 27 30
4 16 20 24 28 32 36 40
5 25 30 35 40 45 50
6 36 42 48 54 60
7 49 56 63 70
8 64 72 80
9 81 90
10 100
The difference is in the space at the start of the lines. Your desired output has 2 spaces for the row number, followed by 2 spaces for the 1
in the row labelled 1
, followed by 4 spaces for each other entry. Mimicking that exactly is a little fiddly — doable, but fiddly:
#include <stdio.h>
int main(void)
{
for (int i = 1; i <= 10; i++)
printf("%4d", i);
putchar('\n');
for (int i = 1; i <= 10; i++)
{
printf("%-*d", 4 * i - ((i == 1) ? 2 : 4), i);
for (int j = i; j <= 10; j++)
printf("%*d", (j == 1) ? 2 : 4, i * j);
putchar('\n');
}
return 0;
}
The conditional expressions are not very elegant.
Upvotes: 0
Reputation: 97928
This may not be exactly what you want but it should help you:
#include <stdio.h>
int main() {
int i, j, result;
printf(" 1 2 3 4 5 6 7 8 9 10\n");
for(i=1; i<=10; i++) {
printf("%3d %*s", i, i * 4, " ");
for (j=i; j<=10; j++) {
printf("%3d ", i * j);
}
printf("\n");
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
1 1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 9 12 15 18 21 24 27 30
4 16 20 24 28 32 36 40
5 25 30 35 40 45 50
6 36 42 48 54 60
7 49 56 63 70
8 64 72 80
9 81 90
10 100
Upvotes: 3