Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Java Exercises: Find the area and perimeter of a circle

Java Basic: Exercise-11 with Solution

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:

Java Code:


public class Exercise11 {
 
   private static final double radius = 7.5;

    public static void main(String[] args) {

        double perimeter = 2 * Math.PI * radius;
        double area = Math.PI * radius * radius;

        System.out.println("Perimeter is = " + perimeter);
        System.out.println("Area is = " + area);
    }
}

Sample Output:

Perimeter is = 47.12388980384689                                                                              
Area is = 176.71458676442586

Flowchart:

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

Sample Solution:

Java Code:

import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  Scanner io = new Scanner(System.in);
  System.out.println("Input the radius of the circle: ");
  double radius = io.nextDouble();
  System.out.println("Perimeter is = " + (2 * radius * Math.PI));
  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

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to compute a specified formula.
Next: Write a Java program that takes five numbers as input to calculate and print the average of the numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe