Calculate the volume of a sphere. - Java Language Basics

Java examples for Language Basics:double

Description

Calculate the volume of a sphere.

Demo Code

import java.util.Scanner;

public class Main
{
   // obtain radius from user and display volume of sphere
   public static void main(String[] args)
   {//from   w  w w  .  j  a va  2 s.co  m
      Scanner input = new Scanner(System.in);

      System.out.print("Enter radius of sphere: ");
      double radius = input.nextDouble();

      System.out.printf("Volume is %f%n", sphereVolume(radius));
   } 

   // calculate and return sphere volume
   public static double sphereVolume(double radius)
   {
      double volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
      return volume;
   } 
}

Result


Related Tutorials