Java Program to find the area of a pentagon
Program
import java.util.Scanner;
public class AreaOfPentagon {
public static void main(String args[]) {
int apothem, side;
float area, area_num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the apothem:\t");
apothem = sc.nextInt();
System.out.println("Enter the length of side:\t");
side = sc.nextInt();
area_num = (5 * side * apothem);
area = area_num / 2;
System.out.println("Area of pentagon: " + area);
}
}
Purpose
This program calculates the area of a pentagon using its apothem and the length of a side. The formula for the area of a regular pentagon is:
[ \text{Area} = \frac{1}{2} \times \text{Perimeter} \times \text{Apothem} ]
1. Declare Variables
int apothem, side;
float area, area_num;
apothem
: Stores the apothem of the pentagon (distance from the center to the midpoint of a side).side
: Stores the length of one side of the pentagon.area_num
: Used to calculate the numerator of the area formula.area
: Stores the final calculated area of the pentagon.
2. Take Input
Scanner sc = new Scanner(System.in);
System.out.println("Enter the apothem:\t");
apothem = sc.nextInt();
System.out.println("Enter the length of side:\t");
side = sc.nextInt();
- Step 1: Create a
Scanner
object to read user input. - Step 2: Prompt the user to enter the apothem and store the input in the variable
apothem
. - Step 3: Prompt the user to enter the length of a side and store it in the variable
side
.
3. Calculate the Area
area_num = (5 * side * apothem);
area = area_num / 2;
-
Step 1: Calculate the numerator: [ \text{area_num} = 5 \times \text{side} \times \text{apothem} ]
- Explanation:
5 * side
computes the perimeter of the pentagon (5 sides of equal length). - Multiply the perimeter by the apothem.
- Explanation:
-
Step 2: Compute the final area: [ \text{area} = \frac{\text{area_num}}{2} ]
4. Display the Area
System.out.println("Area of pentagon: " + area);
- Outputs the calculated area to the console.
Execution Steps
- Input:
apothem = 5
,side = 6
. - Calculate:
- Perimeter: ( 5 \times 6 = 30 )
- Numerator: ( 30 \times 5 = 150 )
- Area: ( \frac{150}{2} = 75.0 )
- Output:
Area of pentagon: 75.0
Output
Enter the apothem:
7
Enter the length of side:
10
Area of pentagon: 175.0