Outputting a Table of Math Stuff

Though I’m admittedly not good at math (at least according to my grades in school), I enjoy playing with math stuff on the computer. Programming allows me to mess with numbers and values without the risk of that ugly red mark and the need to wonder how a D or an F on my transcript affects my overall GPA.

Of all the math I suffered in school, I enjoyed geometry the best. I know, for example, that the sum of the internal angles of a triangle is 180°. For a square, the sum in 360°. The formula for the number of sides on a polygon to yield the total of all the internal angles is: (n - 2) * 180.

This formula makes sense. A two-sided object would have no internal angles. A one-sided object would have seriously messed-up mathematical results as the sum of internal angles.

To obtain the number of degrees for each angle, you divide the result of (n - 2) * 180 by n, which is what I did in the following code:

2025_05_24-Lesson.c

#include <stdio.h>

int main()
{
    int sides,degrees;

    printf("Sides\tAngles\tTotal (degrees)\n");
    for(sides = 3; sides<=12; sides++ )
    {
        degrees = (sides - 2) * 180;
        printf("%5d\t%6d\t%5d\n",
                sides,
                degrees/sides,
                degrees
              );
    }

    return 0;
}

The statement degrees = (sides - 2) * 180; stores the total number of angles for a sides-sided object. This value, along with variable sides, is output in the printf() statement, along with the size of each internal angle:

Sides   Angles  Total (degrees)
    3       60    180
    4       90    360
    5      108    540
    6      120    720
    7      128    900
    8      135   1080
    9      140   1260
   10      144   1440
   11      147   1620
   12      150   1800

The point here isn’t this simple program, but how to craft the table, shown above. Often I struggle as a programmer to present such information in an orderly fashion, to ensure that all the columns line up without having to resort to fancy math or a library like Ncurses to precisely set the cursor and present the details.

Here’s the trick I use:

Write the table header first:

printf("Sides\tAngles\tTotal (degrees)\n");

Each of the column headings is separated by a tab, \t. When outputting the columns in the table, separate each by tab, but use width values in the placeholders that match the character length from the column heading:

Sides has 5 letters
Angles has 6 letters
Total has 5 letters

Here is the printf() formatting string to output each row in the table:

"%5d\t%6d\t%5d\n"

Each %d placeholder has the same width value as its corresponding header: 5, 6, and 5. The tab escape character is sandwiched between each placeholder. The output worked for me the first time without the need to go back and adjust things — which I’ve often suffered doing in the past.

3 thoughts on “Outputting a Table of Math Stuff

  1. Oh yeah! I see that now. The columns are mislabeled and in the wrong order. Thanks!

  2. UPDATE The output shown in the original post didn’t reflect the code’s output. I have updated the post to reflect the code’s correct output. Thanks, Chris!

Leave a Reply