Java Program to Calculate Diameter, Area and Perimeter of a Circle

Area, Perimeter of Circle

Java program to calculate diameter, area and perimeter of a circle has been given below. Suppose, the radius of a circle is r unit, the area and perimeter of that circle would be (pi * r^2) unit^2 and (2 * pi * r) unit, respectively while the diameter would be 2 * r. The approximate value of pi is 3.141592.


For example, if the radius of a circle is 5 cm, then area of the circle would be (pi * 5^2) = 78.54 cm^2 while perimeter and diamerter would be (2 * pi * r) = 31.42 cm and (2 * r) = 10 cm respectively.


The algorithm, pseudocode, and time-complexity of the program have also been covered below.





1. Algorithm to calculate diameter, area and perimeter of a circle


1. Take the radius r of a circle as input.

2. Compute d = 2 * r

3. Compute a = pi * r^2

4. Compute p = 2 * pi * r

5. Declare d as the diameter, a as the area and p as the perimeter of that circle.




2. Pseudocode to calculate diameter, area and perimeter of a circle


Input : Radius r of a circle

Output : Diameter D, Area A, Perimeter P of the circle

1. Procedure diameterAreaPerimeter(r):

2. D := 2 * r

3. A := pi * r^2

4. P := 2 * pi * r

5. Return D, A, P

6. End Procedure





3. Time complexity to calculate diameter, area and perimeter of a circle


Time Complexity: O(1)




4. Java Program & Output to calculate diameter, area and perimeter of a circle

Code has been copied
/*************************************
        alphabetacoder.com
 Java Program program to calculate diameter, 
   area and perimeter of a circle
********************************************/

import java.util.Scanner;
import java.lang.Math;

class Main {
    public static void main(String args[]) {
        // declare object of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        double r, d, a, p;

        //take input of the order of the matrix
        System.out.print("Enter the radius of a circle: ");
        r = sc.nextDouble();

        // calculare diameter
        d = 2 * r;
        // calculate area
        a = Math.PI * r * r;
        // calculate perimeter
        p = 2 * Math.PI * r;

        // display result upto 3 decimal places
        System.out.println("Diameter: " + Math.round(d * 1000) / 1000.0);
        System.out.println("Area: " + Math.round(a * 1000) / 1000.0);
        System.out.println("Perimeter: " + Math.round(p * 1000) / 1000.0);
    }
}

Output


Enter the radius of a circle: 3

Diameter: 6.0

Area: 28.274

Perimeter: 18.85