C# Program to calculate area of pentagon
Program
using System;
namespace area_of_pentagon
{
class AreaOfPentagon
{
static void Main(String[] args)
{
int apothem, side;
float area, area_num;
Console.Write("Enter the apothem:\t");
apothem = int.Parse(Console.ReadLine());
Console.Write("Enter the length of side:\t");
side = int.Parse(Console.ReadLine());
area_num = (5 * side * apothem);
area = area_num / 2;
Console.WriteLine("Area of pentagon: " + area);
}
}
}
This C# program calculates the area of a pentagon using the apothem and side length provided by the user.
-
using System;
- Imports the
System
namespace, allowing the use of built-in functions likeConsole.WriteLine()
.
- Imports the
-
namespace area_of_pentagon
- Defines a namespace named
area_of_pentagon
to organize the class.
- Defines a namespace named
-
class AreaOfPentagon
- Declares a class named
AreaOfPentagon
, which contains the program logic.
- Declares a class named
-
static void Main(String[] args)
- The
Main
method serves as the entry point of the program.
- The
-
Declaring Variables:
int apothem, side;
→ Stores the apothem and side length of the pentagon.float area, area_num;
→ Stores intermediate and final results.
-
Reading User Input:
-
Console.Write("Enter the apothem:\t");
- Displays a prompt to enter the apothem.
-
apothem = int.Parse(Console.ReadLine());
- Reads user input as a string, converts it to an integer, and stores it in
apothem
.
- Reads user input as a string, converts it to an integer, and stores it in
-
Similarly, the side length is read and stored in
side
.
-
-
Calculating the Area:
- Formula: Area of a pentagon = (5 × side × apothem) / 2
area_num = (5 * side * apothem);
- Computes the numerator
(5 × side × apothem)
.
- Computes the numerator
area = area_num / 2;
- Divides by 2 to get the final area.
-
Displaying the Result:
Console.WriteLine("Area of pentagon: " + area);
- Prints the calculated area.
Key Points:
- The apothem is the perpendicular distance from the center to a side.
- The side length is the length of one side of the pentagon.
- This formula applies only to regular pentagons (all sides and angles are equal).
- The program assumes that input values are always valid (i.e., positive numbers).
Output
$ mcs AreaOfPentagon.cs
$ mono AreaOfPentagon.exe
Enter the apothem: 7
Enter the length of side: 15
Area of pentagon: 262.5