# Hackety Hacking Problem AreaofN SidedPolygon Methods

### From OLPC

Revision as of 04:59, 6 April 2008 by CharlesMerriam (Talk | contribs)

## Contents |

## Problem Statement

Write a program to calculate the Area of a N sided Regular polygon of given length.

A regular polygon is a Convex polygon having all sides equal and all angles hence equal to each other.

The area of the regular polygon = N/4 * (L^2) * (1/tan(PI/N)

## Sample Output

You entered sides of polygon = 4 You entered length of each side = 2 The Area of 4 Sided Polygon is 9.000002

## Curriculum Connection

To solve this problem students will

- Take input from user while displaying a message in waiting state
- Make a
*Method*and pass it the values as provided by user - Call the
*Method*with the actual arguments - Print the output

## Solution in RUBY

#To calculate Area of N-Sided Regular Polygon using Methods # Take the Measurements of the Polygon from user say("Enter Number of Sides of Polygon\n"); # Display a friendly message while waiting for user input N = gets.to_f; # Store the user input in floating point variable print("You entered sides of polygon = ", N,"\n"); say("Enter the length of each side \n"); # Request user for polygon data L = gets.to_f; # Store input as floating point variable print("You entered length of each side = ", L,"\n"); def poly_area(n,l) # Defining a method with 2 parameters n , l # Note that n,l are formal arguments , whereas N,L are actual arguments area = (n/4)*(1/(Math.tan(3.141592653/n)))*l*l; # Using the Universal Area Formula : Area = N/4 * ( L^2) * Cot( PI / N) print("The Area of ",N.to_i ," Sided Polygon is ",area,"\n") # Displaying the data after calculation end poly_area(N,L) # This line is calling the method poly_area with 2 parameters N , L