Program to compute the area and perimeter of Octagon

Problem statement:-  Program to compute the area and perimeter of the Octagon.

Formula:

Area Of Octagon(A):


A = 2(1+⎷2)a²

where a=side length of the Octagon

Perimeter of Octagon(P):

P
=
8a

 Data requirement:-

   Input Data:- a

  Output Data:- area, perimeter

Program in C

Here is the source code of the C Program to compute the area and perimeter of the Octagon.

Code:

#include<stdio.h>
#include<math.h>
int main()
{
    printf("Enter the length of the side:");
    int a;
    scanf("%d",&a);
    double area=(2*(1+sqrt(2))*pow(a,2));
    int perimeter=(8*a);
    printf("Area of the Octagon = %lf\n",area);
    printf("Perimeter of the Octagon = %d",perimeter);
}

Input/Output:
Enter the length of the side:5
Area of the Octagon = 120.710678
Perimeter of the Octagon = 40

Program in C++

Here is the source code of the C++ Program to compute the area and perimeter of the Octagon.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    cout<<"Enter the length of the side:";
    int a;
    cin>>a;
    double area=(2*(1+sqrt(2))*pow(a,2));
    int perimeter=(8*a);
    cout<<"Area of the Octagon = "<<area;
    cout<<"\nPerimeter of the Octagon = "<<perimeter;
}

Input/Output:
Enter the length of the side:7
Area of the Octagon = 236.593
Perimeter of the Octagon = 56

Program in Java
  
Here is the source code of the Java Program to compute the area and perimeter of the Octagon.

Code:

import java.util.Scanner;
public class Area_Perimeter_Octagon {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
System.out.println("Enter the length of the side:");
    int a=cs.nextInt();
    double area=(2*(1+Math.sqrt(2))*Math.pow(a,2));
    int perimeter=(8*a);
    System.out.println("Area of the Octagon = "+area);
    System.out.println("Perimeter of the Octagon = "+perimeter);
cs.close();
}}

Input/Output:
Enter the length of the side:
10
Area of the Octagon = 482.84271247461896
Perimeter of the Octagon = 80


Program in Python
  
Here is the source code of the Python Program to compute the area and perimeter of the Octagon.

Code:

import math
print("Enter the length of the side:")
a=int(input())
area=(2*(1+math.sqrt(2))*math.pow(a,2))
perimeter=(8*a)
print("Area of the Octagon = ",area)
print("Perimeter of the Octagon = ",perimeter)

Input/Output:

Post a Comment

0 Comments