Hello all,

i have a little problem printing the upper symmetry of the multiplication as shown in the attachment (in C).

can anyone help?

Dani AI

Generated

The "upper half" of an n x n multiplication table is the cells with column >= row (j >= i). Two common ways to produce that are: shrink the inner loop bounds so each row prints fewer columns, or keep a full grid and print blank fields for j < i. showed the shrinking-inner-loop approach; to "display the other way round" you can instead print blank fields for the lower half and either keep the normal top-to-bottom order or reverse the outer loop to flip the triangle vertically.

Example: upper-triangle with blanks (top row is longest, bottom row shortest)

#include <stdio.h>

int main(void)
{
    int N = 9, i, j;
    for (i = 1; i <= N; ++i) {
        for (j = 1; j <= N; ++j) {
            if (j < i)
                printf("%4s", "");
            else
                printf("%4d", i * j);
        }
        putchar('\n');
    }
    return 0;
}

To get the "other way round" (short rows at the top, long rows at the bottom) simply reverse the outer loop; the cell test stays the same:

for (i = N; i >= 1; --i) {  /* rest is identical to the previous loop body */ }

Notes: use a fixed field width (eg. "%4d") so columns stay aligned. If you want no empty columns at all, use an inner loop that starts at j = i and runs to N (that prints only the numeric cells, left-aligned). Adjust N and field width for larger tables so products don't overlap. , try the reversed-outer-loop version if your attachment showed the triangle flipped vertically.

Recommended Answers

All 4 Replies

Um, upper symmetry? Would you care to define that for us?

hello, thanks for the reply.

i meant only the upper half of the table. i have displayed the lower half (as you can see from the attached file) and i can display the full table. I'm trying to display only the upper half.

thanks

You mean like this?

#include <stdio.h>

#define LIMIT 10

int main ( void )
{
  int x;
  int y;

  for ( x = 1; x < LIMIT; x++ ) {
    for ( y = 1; y < LIMIT - x + 1; y++ )
      printf ( "%5d", x * y );

    puts ( "" );
  }
}

i got this when i was trying it. i want it to display the other way round( see the attached)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.