w3resource

Java: Find the area and perimeter of a circle


Circle: Area and Perimeter

Write a Java program to print the area and perimeter of a circle.

In geometry, the area enclosed by a circle of radius r is πr2. Here the Greek letter π represents a constant, approximately equal to 3.14159, which is equal to the ratio of the circumference of any circle to its diameter.

The circumference of a circle is the linear distance around its edge.

Pictorial Presentation:

Java area and perimeter of a circle


Why is the area of a circle of a circle pi times the square of the radius? 

Why is the area of a circle of a circle pi times the square of the radius?


Sample Solution-1

Java Code:

public class Exercise11 {
    // Define a constant for the radius of the circle
    private static final double radius = 7.5;

    public static void main(String[] args) {
        // Calculate the perimeter of the circle using the constant radius
        double perimeter = 2 * Math.PI * radius;

        // Calculate the area of the circle using the constant radius
        double area = Math.PI * radius * radius;

        // Print the calculated perimeter and area
        System.out.println("Perimeter is = " + perimeter);
        System.out.println("Area is = " + area);
    }
}

Explanation:

In the exercise above -

The above Java code calculates and prints the perimeter and area of a circle with a given radius (in this case, a radius of 7.5 units). It uses mathematical constants and formulas for circles:

  • It calculates the perimeter (circumference) using the formula: 2 * Math.PI * radius;
  • It calculates the area using the formula: π * radius^2.

The perimeter and area values are then printed to the console.

Sample Output:

Perimeter is = 47.12388980384689                                                                              
Area is = 176.71458676442586

Flowchart:

Java area and perimeter of a circle


Sample Solution-2

Java Code:

import java.util.Scanner;

public class Main {
 public static void main(String[] args) {
  // Create a Scanner object to read input from the user
  Scanner io = new Scanner(System.in);
  
  // Prompt the user to input the radius of the circle
  System.out.println("Input the radius of the circle: ");
  
  // Read and store the input radius
  double radius = io.nextDouble();
  
  // Calculate and print the perimeter of the circle
  System.out.println("Perimeter is = " + (2 * radius * Math.PI));
  
  // Calculate and print the area of the circle
  System.out.println("Area is = " + (Math.PI * radius * radius));
 }
}

Sample Output:

Input the radius of the circle: 
 5
Perimeter is = 31.41592653589793
Area is = 78.53981633974483

Flowchart:

Flowchart: Java exercises: Find the area and perimeter of a circle


For more Practice: Solve these Related Problems:

  • Write a program to calculate the circumference and area of an ellipse.
  • Modify the program to calculate the area and perimeter of a semicircle.
  • Write a program to compare the areas of two circles given their radii.
  • Calculate the surface area and volume of a sphere.

Go to:


PREV : Formula Computation.
NEXT : Average of Three Numbers.


Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.