JavaUsing the Java Math Package to Compute an Area

Using the Java Math Package to Compute an Area

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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();
		usingMath.proceed();
	}
	
	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:

[root@mypc]# java UsingMath
Computing area of a circle
Area of a circle with radius 3.0 is 28.274333882308138

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories