Java Program to Find Area of Octagon

In the previous article, we have seen Java Program to Find Area of Enneagon

In this article we are going to see how to find the area of octagon using Java programming language.

Java Program to Find Area of Octagon

Before Jumping into the program directly let’s see how we can find the area of octagon.

Explanation:

Regular octagon is a closed figure having 8 sides of equal length and equal internal triangle.

Formula to find area of octagon (approx.) = 2(1+√2)*s*s   where 's' represents side length of octagon

i.e Area = 2 × (side length of octagon)² × (1+sqrt(2))

Example:

Let one of the sides of octagon be “s” = 1

So, vol. of octagon = 2(1+√2)*s*s = = 2(1+√2)*1*1 = = 2(1+√2) = 4.8284

Let’s see different ways to find area of octagon.

Method-1: Java Program to Find Area of Octagon By Using Static Value

Approach:

  • Declare an double variable say “s”, assign the value to it, which holds the value for one of the side of octagon.
  • Find the surface area of octagon using the formula  2(1+√2)*s*s
  • Print the result.

Program:

import java.io.*;
class Main
{
    public static void main(String [] args)
    {
        //side length of octagon declared
        double s = 1;
        //finding area using formula
        double area  = 2*(1+Math.sqrt(2))*s*s;
        System.out.println("The area of octagon is: " + area);
    }
}
Output:

The area of octagon is: 4.82842712474619

Method-2: Java Program to Find Area of Octagon By Using User Input Value

Approach:

  • Declare an integer variable say “s”, assign the value to it, which holds the value for one of the side of octagon.
  • Find the surface area of octagon using the formula  2(1+√2)*s*s
  • Print the result.

Program:

import java.util.*;
class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner sc = new Scanner(System.in);
        //Taking input of side length
        System.out.println("Enter the value of one side of the octagon:");
        double s = sc.nextInt();

        //finding area using formula
        double area  = 2*(1+Math.sqrt(2))*s*s;
        System.out.println("The area of octagon is: " + area);
    }
}
Output:

Enter the value of one side of the octagon:
2
The area of octagon is: 19.31370849898476

Method-3: Java Program to Find Area of Octagon By Using User Defined Method

Approach:

  • Declare an integer variable say “s”, assign the value to it, which holds the value for one of the side of octagon.
  • Call the user defined method say findArea() and pass the side length i.e. ‘s‘ as parameter.
  • Inside the method find the surface area of octagon using the formula  2(1+√2)*s*s
  • Print the result.

Program:

import java.util.*;
class Main
{
    public static void main(String [] args)
    {
        //Scanner class object created
        Scanner sc = new Scanner(System.in);
        //Taking input of side length
        System.out.println("Enter the value of one side of the octagon:");
        double s = sc.nextInt();
        //calling the findArea() method
        findArea(s);
    }
    
    public static void findArea(double s)
    {
        //finding area using formula
        double area  = 2*(1+Math.sqrt(2))*s*s;
        System.out.println("The area of octagon is: " + area);
    }
}
Output:

Enter the value of one side of the octagon:
4
The area of octagon is: 77.25483399593904

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Articles: