Home > database >  Please help me to correct the code in C program
Please help me to correct the code in C program

Time:01-15

Must be exactly like on the 2nd picture. I tried to put \t before 1 but it doesn`t work, also I can't type -------------- after the multiplication table.

enter image description here

enter image description here

int main(int argc, char* argv)
{
    puts("| 1 2 3 4 5 6 7 8 9 ");
    puts("-------------------------------------");

    for (int x = 1; x <= 9; x  )
    {
        for (int y = 1; y <= 9; y  )
        {
            if (y < x)
            {
                printf("    ");
            }
            else
            {
                if (y < 10)
                {
                    printf("M",x*y);
                }
                else
                {
                    printf("m",x*y);
                }
            }
        }

        puts("");
    }
}

CodePudding user response:

You probably want this:

#include <stdio.h>

int main(int argc, char* argv)
{
  puts("|  1   2   3   4   5   6   7   8   9 ");  // change this line
  puts("-------------------------------------");

  for (int x = 1; x <= 9; x  )
  {
    for (int y = 1; y <= 9; y  )
    {
      if (y < x)
      {
        printf("    ");
      }
      else
      {
        if (y < 10)
        {
          printf("M", x * y);
        }
        else
        {
          printf("m", x * y);
        }
      }
    }

    puts("");
  }
  puts("\n-------------------------------------");  // add this line
}

CodePudding user response:

No need for check if (y < 10), since you are looping y from 1 to 9 only.

#include <stdio.h>

int main(int argc, char* argv)
{
    puts("  1  2  3  4  5  6  7  8  9");
    puts("--------multiplication table----");

    for (int x = 1; x <= 9; x  )
    {
        for (int y = 1; y <= 9; y  )
        {
            if (y < x)
            {
                printf("   ");
            }
            else
            {
                printf("=",x*y);
            }
        }

        puts("");
    }
    puts("-------------------------------");
}
  •  Tags:  
  • Related