Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

How to get the perimeter of a circle in Java

okwudili onyejiaku

Overview

The perimeter of a circle is its boundary or the total distance around a circle. The term that denotes the perimeter is called the circumference of a circle. Mathematically, the perimeter of a circle is 2*Pi*radius.

Syntax

2 * PI * radius
Syntax of perimeter of a circle in Java

Parameters

  • PI: This is the mathematical pi. It is equal to 3.14.
  • radius : This is the radius of the circle.

Return value

The value returned is a float value.

Example

import java.lang.Math;
class HelloWorld {
    public static void main( String args[] ) {
        // create some radii
        float rad1 = 3;
        float rad2 = 45;
        float rad3 = 1.5F;
        float rad4 = 0.1F;

        // get the perimeter of cricles
        double per1 = 2 * Math.PI * rad1;
        double per2 = 2 * Math.PI * rad2;
        double per3 = 2 * Math.PI * rad3;
        double per4 = 2 * Math.PI * rad4;

        // print the perimeters
        System.out.println("The perimeter of circle with radius "+rad1+" is: "+per1);
        System.out.println("The perimeter of circle with radius "+rad2+" is: "+per2);
        System.out.println("The perimeter of circle with radius "+rad3+" is: "+per3);
        System.out.println("The perimeter of circle with radius "+rad4+" is: "+per4);
    }
}
Get the perimeter of a circle in Java

Explanation

  • Line 1: We import the Math module.
  • Lines 5–8: We create some radius values.
  • Lines 11–14: We get the perimeter of circles with the radius created.
  • Lines 17–20: We print the perimeters.

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring