Skip to content

Instantly share code, notes, and snippets.

@imryan
Last active April 12, 2023 18:28
Show Gist options
  • Select an option

  • Save imryan/7087188 to your computer and use it in GitHub Desktop.

Select an option

Save imryan/7087188 to your computer and use it in GitHub Desktop.
Calculates volume and surface area of a sphere's radius.
import java.util.Scanner;
import java.text.*;
public class Sphere
{
public static void main(String[] args)
{
// Declare variables
Scanner sc = new Scanner(System.in);
final double PI = Math.PI;
double radius = 0.0;
double volume, surfaceArea;
// Get input
radius = sc.nextDouble();
// Calculate volume/area
volume = Math.pow(((4/3) * PI * radius), 3);
surfaceArea = Math.pow((4 * PI * radius), 2);
// 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