Last active
April 12, 2023 18:28
-
-
Save imryan/7087188 to your computer and use it in GitHub Desktop.
Calculates volume and surface area of a sphere's radius.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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