Question

C++ program

A regular polygon is an n-side polygon in which all sides are of the same length and all angles have the same degree(i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is

image.png

Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area.

 


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since the area of the polygon is s*s*n / (4 * tan(1800 / n)), the code is implemented in c++.

C++ code:

// Import necessary libraries
#include 
#include 
#include 

using namespace std; 

int main() {
    cout << "Enter the number of sides and the length of regular polygon separated by a space" << endl;
    
    // Take input
    float s;
    int n;
    cin >> n >> s;

    // Calculate area by using the formual
    float area = (s * s * n) / (4 * tan(M_PI / n));

    // Print the output
    cout << "The area of the regular polygon is " << area << endl;
    return 0;
}

Please refer to the following picture for the source code:

1 2 3 4 // Import necessary Libraries #include <stdio.h> include <math.h> #include cbits/stdce.h 6 7 using namespace std: int

Sample execution of the above code:

Enter the number of sides and the length of regular polygon separated by a space 45 The area of the regular polygon is 25

Add a comment
Know the answer?
Add Answer to:
C++ program
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A regular polygon is an n-sided polygon in which all sides are of the same length...

    A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon isArea =Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area. Here is a sample run:

  • Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on...

    Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on page 362, reprinted below 9.9 (Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides have the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular) Design a class named RegularPolygon that contains A private int data field named n that defines the number of sides in the polygon with default value of 3 A...

  • (Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all...

    (Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all of its angles have the same degree (i.e., the polygon is both equilateral and equiangular). Design a class named RegularPolygon that contains: ■ A private int data field named n that defines the number of sides in the polygon. ■ A private float data field named side that stores the length of the side. ■ A private float data field named x that defines...

  • //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of the same length...

    //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). Design a class named RegularPolygon that contains: Aprivate int data field named n that defines the number of sides in the polygon with default value 3. A private double data field named side that stores the length of the side with default value 1. A private double data field...

  • Write a program that prompts the user to enter the sides and their length of a...

    Write a program that prompts the user to enter the sides and their length of a regular polygon and display its area. ****Using c++

  • N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and...

    N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and whose angles have the same degree. Write a class called NSidedPolygon that contains the following components: Private members to store the name of a polygon, the number of sides, and the length of each side. (You are expected to select the appropriate data types for each member.) A constructor that accepts the number of sides and the length of each side. A private method...

  • Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3:...

    Welcome to the Triangles program Enter length 1: 3 Enter length 2: 5 Enter length 3: 5 Enter the base: 5 Enter the height: 8 --------------------- The triangle is Isosceles since it has two equal length sides. Isosceles triangle also has two equal angles The area is: 20 The permimeter is: 13 Continue? (y/n): y Enter length 1: 10 Enter length 2: 10 Enter length 3: 10 Enter the base: 10 Enter the height: 7 --------------------- The triangle is Equilateral...

  • please write psedocodes for all of the questions and an algorithm for 2. no coding is...

    please write psedocodes for all of the questions and an algorithm for 2. no coding is required . FIUJELI 95 PIOL 1. (Geometry: Area of a Pentagon) Write a C# program that prompts the user to enter the length from the center of a pentagon to a vertex and computes the area of the pentagon, as shown in the following figure. The formula for computing the area of a pentagon is Area = 2 , where s is the length...

  • Write a program that displays a convex regular polygon with m or n sides, where m...

    Write a program that displays a convex regular polygon with m or n sides, where m is the last digit of your student number plus 3, n is the second last digit plus 3. Use two buttons named “before” and “after” to change the number of sides of the polygon. Click on “before” button will show a convex regular polygon with m sides and click on “after” will show a convex regular polygon with n sides. For example, a student...

  • Problem a (PA4a.java) Write a program to evaluate the area of a triangle given the lengths...

    Problem a (PA4a.java) Write a program to evaluate the area of a triangle given the lengths of its sides using Heron's Formula. Here is an outline: Get the three side lengths from the user (which might have decimal values): a, b, c. Check to ensure that the sides are valid for a triangle. Importantly, the sum of the lengths of any two sides must be larger than the length of the third side (you must check all three sides this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT