﻿ Java exercises: Find the area and perimeter of a circle - 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: 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: 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: Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

﻿