Search code examples
cfor-loopnested-loopsmultiplication

C Beginner: Multiplication table


I'm new to C and I'm trying to create a simple multiplication table with size based on user input, but I'm having some issues with the output.

How can I make my table include the row and column for number 1?

void main()
{
  int j,i,n;
  printf("Enter the size from 1-10: ");
  scanf("%d",&n);
    
  for (i=1; i<=10; i++)
  {
    for (j=1; j<=n; j++)
    {
      if (j <= n-1)
        printf("%d\t",i*j);
      else
        printf("%d\t",i*j);
    }
    printf("\n");
  }
}

Current output:

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

EDIT: Sorry for writing my question in haste. I want my output to look something like this:

Desired output


Solution

  • So I worked on your problem and finally create the output that you wanted.
    Code is here.

    • So I have done that before printing each character I'm checking the length of the number and according to that I'm printing the number.

    • I do the best approach I can do and it does give desire output.

    [EDITED] : I have updated my code. There was some problem with small numbers ,So try it again on the old link.