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.
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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework