JavaUsing the Java Math Package to Compute an Area

Using the Java Math Package to Compute an Area

The Math package in java.lang is full of resources. Below is some example Java code that computes the area of a circle where PI is a value that is used and may not be provided by the user. Here is how to use the Math.PI in Java:


public class UsingMath
	public static void main(String args[])
		UsingMath usingMath = new UsingMath();
	private void proceed()
		System.out.println("Computing area of a circle");
		float radius = 3;
		double area = Math.PI * radius * radius; 
		System.out.println("Area of a circle with radius " + radius + " is " + area);


Entering the above code in your integrated development environment (IDE) or code editor results in the following output. Note, however, that your output may vary depending on your system settings and operating system:

[[email protected]]# java UsingMath
Computing area of a circle
Area of a circle with radius 3.0 is 28.274333882308138

Latest Posts

Related Stories