Problems with polygons in Pascal!!!

I have an assignment which deals primarily with the computation of various factors of regular polygons. The task is as follows:

"Finish writing the following program. Don't make any changes to the main body: all work should be within the PROCEDURES and FUNCTIONS. The program shows a menu asking the user to choose one of the six regular polygons (an equilateral triangle, a square, a regular pentagon, a regular hexagon, etc). It then asks the user for the length of one side of the chosen figure. It then calculates the perimeter, the longest distance across, and the area of the figure, and outputs these to screen. It repeats this until the user decides to quit.
Here's the main body of the program. Complete it by writing the program declarations, including all the procedures and functions referred to in the main body.

BEGIN {main body}
      choice := GetChoice;
      CASE choice OF
         1: Triangle(side, perimeter, across, area);
         2: Square(side, perimeter, across, area);
         3: Pentagon(side, perimeter, across, area);
         4: Hexagon(side, perimeter, across, area);
         5: Septagon(side, perimeter, across, area);
         6: Octagon(side, perimeter, across, area);
       END; {of CASE}
       IF choice <> 7 THEN BEGIN
           write('Side =      ' side:8:2);
           write('Perimeter = ' perimeter:8:2);
           write('Across =    ' across:8:2);
           write('Area =      ' area:8:2) END;
   UNTIL choice = 7;

I have attempted to use the following formula (obtained from: to solve for the area of any given regular polygon:

area := n*(s*s)*(((cos((360/n)/2))/(sin((360/n)/2)))/4);

where n = no. of sides, and s = length of side.

In a step-by-step logical computation, this formula (confusing as it is) should work (substitute values and you'll see). However, as Pascal gives the result of sin and cos as radian rather than degree values, it does not.

perimeter of the object is given by:

perimeter := s*n

Any sort of clarification as to working Pascal formulae for the object's area and longest distance across would be extremely welcome.

I'm new to how all this works so please get in touch with me to clarify anything.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dfordeAuthor Commented:
Edited text of question.
area := n*(s*s)*(((cos((360/n)/2))/(sin((360/n)/2)))/4);

If the only problem you are having is the degrees/radians thing, it is fairly simple to convert it:

Let's start with (cos((360/n)/2))

There are 360 degrees in a circle.  There are 2*pi radians in a circle.  So all we have to do is substitute 360 with 2*pi.

It becomes


This can be further can cancel the 2 in the numerator and denominator out:


You can do the same thing with the sin.

So the whole formula becomes:

area := n*(s*s)*((cos(pi/n)/(sin(pi/n))/4;

This can be simplified further:  tan x = sin x/cos x, so cos x/sin x = 1/tan x

area := n * (s*s) * (1/tan(pi/n)) / 4

Simplified yet again:

area := n * (s*s) / (4 * tan(pi / n));

I don't know if your original formula is correct, but the formula directly above should be the equivalent, using radians.

So far we have:

area := n * (s*s) / (4 * tan(pi / n));
perimeter := s * n;

Longest distance across, hmmmm....

Give me some time to think about that one...

The longest distance across a triangle is:

d := s*sqrt(3)/2;


d := s*sqrt(2);
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

This is off the top of my head so it may not be right.

This should be all triangle calculation.

These are regular po;ygons so a circle should enclose them touching all the corners of the po;ygons.  From the centre of the circle you can join all of the corners together forming triangles.  All triangles are the same size.  The angles at the base of the trianges are equal.  You can calculate the opposte angle (360 degrees divided by number of polygon sides).  You can then calculate the other two angles.  You know the size of the base - this touches the circle - so you can then calculate the other two sides - these are actually the radius of the circle.

The longest distance across is on the diameter.  You know the radius.  For the even sided polygons this will be the diameter of the circle.

For the uneven polygons you need to calculate the height of a triangle - all the data is there - and add it to the radius.

Will this work?  It's off the top of my head.
hmmm.... if i haven't done it wrongly,
longest distance (d) for

even n: d=s/sin(pi/n)
odd n: d=s*sin((n-1)pi/2n)
 / sin(pi/n)

scrapdog: for equilateral triangle the longest length is s, not s*sqrt(3)/2


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
as for the programming part, the format of the main routine expects ur subroutines to change the values of the variables
perimeter, across and area.... u do that by using the VAR keyword... eg.

procedure boo(a: integer; var b, c:integer);
will allow u to change b and c in the procedure (and retain it when u go out of the procedure) but not a (u can still change it inside but the changes will not get 'carried forward')

it may also be easier to write a general routine that is called from triangle blahblah...
>for equilateral triangle the longest length is s, not s*sqrt(3)/2

....but he didn't ask for longest length, he asked for longest distance across.
I (probably falsely) assumed that that meant a line that would pass thru the center.
As your choice statement picks up a value from 1 (triangle = 3 sides) to 6 (octagon = 8 sides), just do a single routine where n (the number of sides) = choice + 2

  If Choice < 7 then
        Polygon(choice+2, side, perimeter, etc...);
dfordeAuthor Commented:
Edited text of question.
Hi guys,
it's clear to me that mathematical problem in this case is more interesting than programmatic one, but anyway - who will write the PROGRAM itself? I could do it (I'm sure - every of you could), but excuse me, the price is very cheap :-))).

dforde, if iou do not want a procedure like this:

procedure N_tagon(AnglesNum: Integer; var Perimeter, Across, Area: Real);

but want a procedure for each figure, you should rise the price :-).

dfordeAuthor Commented:
Thanks to scrapdog for the simplification of the formulae. Had I spent two minutes more examining the formulae given at (which I only did after posting) I should have been able to figure out the conversion of the output to degrees rather than radians by substituting (2*pi/n) in place of (360/n). Nevertheless, your response served as a very useful reminder.

One thing to note though, is that standard Pascal has no predefined function for tan/tangent (or at least I have not yet come across any - please correct me if I'm wrong), therfore, one can only apply the simplification to the level where:
area := n*(s*s)*((cos(pi/n)/(sin(pi/n))/4;

I can personally verify the correct workings of the above formula. However, can anyone verify the formulae given to solve for the longest distance across?
lychee gave a resonable response with:
even n: d=s/sin(pi/n)
odd n: d=s*sin((n-1)pi/2n)/sin(pi/n)

This provides answers that appear valid, but I have not yet completed testing its complete correctness for all given regular polygons. If someone can validate this formulae I'd be very thankful.

Lastly, thanks to Fatman for his offer, however, one should note that in my question I asked for assistance in obtaining the *FORMULAE* for the regular polygon's area and longest distance across *NOT* for someone to do the *PROGRAM* for me! I believe it would break too many ethical codes to have someone *DO* my work for me, not to mention many of my classmates reference this very forum. Thanks nonetheless. :)

As soon as I get firm verification of the validity of the formulae for the longest distance across, I will be happy to review the responses and apply credits where due.
>>standard Pascal has no predefined function for tan/tangent

The same ridiculous thing of having no power operator. You're right; but, as you stated, you can use the equivalent sin/cos.
dfordeAuthor Commented:
I have decided to go with the formulae provided by lychee for the longest distance across being
n: d=s/sin(pi/n) for an even sided regular polygon and for an odd sided figure n: d=s*sin((n-1)pi/2n)/sin(pi/n).

Thanks, however to scrapdog for his simplification of the formula for area being area := n*(s*s)*((cos(pi/n)/(sin(pi/n))/4;

If I could split the points between these two guys I would, but I'm not sure that's possible, so I guess I'll have to lean in lychee's favour for providing a completely original (and seemingly working) solution/formula for the longest distance across a regular polygon.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.