Sanee Reimoo Sanee Reimoo - 21 days ago 5
C Question

how to generate tables using arrays?

#include <stdio.h>
#include <stdlib.h>

int main()
{
system("color f0");
int k,i,j,n;

printf("Generate tables upto:");
scanf("%d",&n);
int tables[n][10];
printf("Table\t");
for(k=1;k<=10;k++)
{
printf("%dx\t",k);
}
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
{
tables[i][j]=i*j;
printf("%d\t",tables[i][j]);
}
}
return 0;
}


this is my code which i am working on but unfortunately i am not able to genrate it the way i want it.
this is what i want. Thanks

Answer

proposal fix for your code

  • C-arrays start at 0
  • you're missing tabs and linefeed at the proper locations

code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    system("color f0");
    int k,i,j,n;

    printf("Generate tables upto:");
    scanf("%d",&n);
    int tables[n][10];
    printf("Table\t");
    for(k=1;k<=10;k++)
    {
        printf("%dx\t",k);
    }
    printf("\n\t");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=10;j++)
        {
            tables[i-1][j-1]=i*j;
            printf("%d\t",tables[i-1][j-1]);
        }
       printf("\n\t");
    }
    return 0;
}

display with n=5

Generate tables upto:5
Table   1x      2x      3x      4x      5x      6x      7x      8x      9x      10x
        1       2       3       4       5       6       7       8       9       10
        2       4       6       8       10      12      14      16      18      20
        3       6       9       12      15      18      21      24      27      30
        4       8       12      16      20      24      28      32      36      40
        5       10      15      20      25      30      35      40      45      50
Comments