Skip to content

Instantly share code, notes, and snippets.

@joeyv
Last active December 26, 2015 10:28
Show Gist options
  • Select an option

  • Save joeyv/7136633 to your computer and use it in GitHub Desktop.

Select an option

Save joeyv/7136633 to your computer and use it in GitHub Desktop.
Calculates Volume and Surface Area of a sphere
import java.util.Scanner;
import java.text.*;
public class Sphere
{
public static void main(String[] args)
{
double pi = Math.PI;
double radius, volume, surfaceArea;
// Get Radius
Scanner scan = new Scanner(System.in);
radius = scan.nextDouble();
// Calculate volume/area
volume = (double)Math.pow((radius), 3) * pi * (4.0/3.0);
surfaceArea = Math.pow((radius), 2) * pi * 4;
// Format values
DecimalFormat df = new DecimalFormat("0.####");
// Output values
System.out.println("Volume: " + df.format(volume));
System.out.println("Surface Area: " + df.format(surfaceArea));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment