Java double type uses 64 bits to store a value.
All transcendental math functions in Java, such as sin()
, cos()
, and sqrt()
, return double values.
Here is a short program that uses double variables to compute the area of a circle:
// Compute the area of a circle. public class Main { public static void main(String args[]) { double pi, r, a; r = 10.8; // radius of circle pi = 3.1416; // pi, approximately a = pi * r * r; // compute area System.out.println("Area of circle is " + a); }/* w ww .ja v a 2 s.c o m*/ }
Java double type calculate area of circle
public class Main { public static void main( String[] args ) {// w w w.j a va 2s . c o m java.util.Scanner input = new java.util.Scanner( System.in ); System.out.print( "Enter to radius: " ); double radius = input.nextDouble(); System.out.printf( "The area is: %.2f%n", circleArea( radius ) ); } public static double circleArea( double radius ) { return Math.PI * Math.pow( radius, 2 ); } }