Java Program to Calculate Volume of Sphere

  • Java Program to find the volume of sphere.
  • Java Program to find the total surface area of sphere.

To find the volume and surface area of a sphere, we want the length of it's radius. Before jumping into the java program, here is the brief summary of spheres.

A sphere is a three dimensional object such that all points on sphere are located at a distance R (radius of sphere) from a given point (center of sphere). A sphere is perfectly symmetrical and has no edges or vertices. Finding volume and surface area of a sphere help us to solve many real life problems like, how much water can be filled in a hollow spherical can.

How to Calculate the Volume of Sphere ? The volume of sphere is defined as the amount of three dimensional space occupied by the sphere. To calculate the volume of a sphere, we need radius of sphere. Volume of sphere is measured in cubic units.
Volume of Sphere = 4/3 x PI x R3
Where, R is the radius of sphere.
How to Find the Total Surface Area of Sphere ?
Surface area of sphere is the number of square units that will exactly cover the outer surface of sphere. There is only one curved surface in sphere, no edges and corners. Total surface area of a sphere is measured in square units like cm2, m2 etc.
Total Surface Area of Sphere = 4ΠR2
Where, R is the radius of sphere.
The total surface area of sphere is four times the area of a circle of same radius.

Java program to find surface area and volume of sphere

In this java program, we first take the radius of sphere as input from user and then calculate the volume and surface of spheres using the formulae mentioned above.

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);
    }
}
Output
Enter Radius of Sphere
5
Surface Area of Sphere = 314.159
Volume of Sphere = 523.599