java calculate volume of sphere code example

Example: java program to calculate Volume of Sphere

package com.tcc.java.programs;
 
import java.util.Scanner;
 
/**
 * Java Program to find volume and surface area of sphere
 */
public class VolumeOfSphere {
 
    public static void main(String[] args) {
        double radius, volume, surfaceArea;
        Scanner scanner;
        scanner = new Scanner(System.in);
        // Take input from user
        System.out.println("Enter Radius of Sphere");
        radius = scanner.nextDouble();
 
        /*
         * Surface area of Sphere = 4 X PI X Radius X Radius
         */
        surfaceArea = 4 * Math.PI * radius * radius;
        /*
         * Volume of Sphere = 4/3 X PI X Radius X Radius X Radius
         */
        volume = (4.0 / 3) * Math.PI * radius * radius * radius;
 
        System.out.format("Surface Area of Sphere = %.3f\n", surfaceArea);
        System.out.format("Volume of Sphere = %.3f\n", volume);
    }
}

Tags:

Java Example