Signup/Sign In

Java Program to Calculate the Surface Area and Volume of Sphere

In this tutorial, we will learn how to find the surface area and volume of the sphere in java. A perfectly symmetrical 3 – Dimensional circular-shaped object is a Sphere. The line that connects from the center to the boundary is called the radius of the sphere. The surface area of a sphere is defined as the region covered by its outer surface in three-dimensional space. The volume of the sphere is defined as the capacity it has. But before moving further, if you are not familiar with the concept of data types in java, then do check the article on the topic Data Types in Java.

Below is the pictorial representation of the sphere.

Input: Enter the radius of a sphere: 5

Output: The surface area of the sphere is: 314

The volume of the sphere is 523.33

Let us look at the examples to know how to find the surface area and volume of the sphere.

Program 1: Find the Surface Area and Volume of Sphere

In this example, we will learn how to calculate the surface area and volume of a sphere in java.

Algorithm:

  1. Start
  2. Create an instance of the Scanner Class
  3. Declare a variable to store the radius of the sphere.
  4. Ask the user to initialize the variable.
  5. Declare variables to store the surface area and volume of the sphere.
  6. Use the formula to calculate the values.
  7. Display the calculated surface area and volume of the sphere
  8. Stop.

The below example demonstrates how to find the surface area and volume of a sphere.

//Java Program to Find the Surface Area and Volume of Sphere
import java.util.*;
  
public class Main 
{
    // Driver Code
    public static void main (String[] args)
    {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter the radius of the sphere: ");
       double radius=sc.nextDouble();
       double surface_area = 4 * 3.14 * (radius * radius);
       double volume = ((double)4 / 3) * 3.14 * (radius * radius * radius);   
        System.out.println("The surface area of the sphere = "+surface_area);   
        System.out.println("The volume of sphere = "+volume);  
    }
}


Enter the radius of the sphere: 10
The surface area of the sphere = 1256.0
The volume of sphere = 4186.666666666667

Program 2: Find the Surface Area and Volume of Sphere

In this example, we will learn how to calculate the surface area and volume of a sphere in java.

Algorithm:

  1. Start
  2. Create an instance of the Scanner Class
  3. Declare variables to store the radius of the sphere.
  4. Ask the user to initialize these variables.
  5. Call two user-defined methods to calculate the surface area and the volume of the sphere.
  6. Pass the entered values as arguments.
  7. Declare variables to store the surface area and volume of the sphere.
  8. Use the formula to calculate the values.
  9. Return the calculated values.
  10. Display the calculated surface area and volume of the sphere.
  11. Stop.

The below example demonstrates how to find the surface area and volume of a sphere

//Java Program to Find the Surface Area and Volume of Sphere
import java.util.*;
  
public class Main 
{
    //Calculate the Volume of Sphere
	public static double VolumeOfSphere (double radius) 
	{
		double Volume = (4.0 / 3) * Math.PI * radius * radius * radius;
		return Volume;
	}
	//Calculate the Surface Area of Sphere
	public static double SurfaceAreaOfSphere (double radius) 
	{
		double surfacearea =  4 * Math.PI * radius * radius;
		return surfacearea;
	}
	// Driver Code
    public static void main (String[] args)
    {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter the radius of the sphere: ");
       double radius=sc.nextDouble();
       double surface_area = SurfaceAreaOfSphere(radius);
       double volume = VolumeOfSphere(radius);
       System.out.println("The surface area of the sphere = "+surface_area); 
       System.out.println("The volume of sphere = "+volume);
       
    }
}


Enter the radius of the sphere: 4
The surface area of the sphere = 201.06192982974676
The volume of sphere = 268.082573106329



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.