Welcome to the new tutorial Java Program to calculate Surface Area and Volume of Sphere of the Java Geometrical Calculation Programs series. In this post, we will learn to calculate the surface area and volume of the sphere in Java.
The previous tutorial of this series is Java Program to calculate volume of cone. Now here we will write a program to find the area and volume of the sphere in Java.
We can calculate the surface area and volume of the sphere using the following mathematical formula.formula.
Surface area of sphere, A = 4 π r2
Where,
- π is a constant whose value is 3.1415 or 22/7
- r is the radius of the sphere.
Volume of sphere, V = (4 ⁄ 3) π r3
Where,
- π is a constant whose value is 3.1415 or 22/7
- r is the radius of the sphere.
Now let’s write the program.
Java Program to calculate Surface Area and Volume of Sphere
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.*; public class Sphere { public static void main(String[] args) { double r, area, volume; Scanner in = new Scanner(System.in); // Asking the user to enter the radius of the sphere System.out.println(" Enter the radius of the sphere :"); r = in.nextDouble(); area = 4 * Math.PI * r * r; // Calculating surface area of sphere volume = (4 * Math.PI * r * r)/3; // Calculating volume of sphere System.out.println(" Surface area of sphere : " + area +" square units"); System.out.println(" Volume of sphere : " + volume +" cubic units"); } } |
What we did ?
- First of all we have imported the util package.
- Then created a class and named it Sphere.
- After that started main() method of the program.
- Then inside main() method declared variables.
- Then created object of the Scanner class.
- Now asked the user to enter the radius of the sphere. Then read the value of radius of the sphere entered by the user.
- Then calculated surface area of the sphere using the formula area = 4 * Math.PI * r * r.
- After that calculated volume of the sphere using the formula volume = (4 * Math.PI * r * r)/3.
- Then finally displayed the value of the surface area and the volume of the sphere on the screen.
Output:
1 2 3 |
Enter the radius of the sphere :2 Surface area of sphere : 50.26548245743669 square units Volume of sphere : 16.755160819145562 cubic units |
The Java program to calculate surface area and volume of sphere has been successfully completed. In the next tutorial, we will learn Java Program to calculate Total Surface Area and Curved Surface Area of cylinder.
Related Tutorials…
- Java Program to Calculate Area and Circumference of Circle
- Java Program to calculate Area of Triangle
- Java Program to calculate Area of Triangle using heron’s formula
- Java Program to calculate Area and Perimeter of Right Triangle
- Java Program to calculate Area and Perimeter of Rectangle
- Java Program to calculate Area and Perimeter of Square
- Java Prorgram to calculate Area and Perimeter of Parallelogram
- Java Program to calculate Area of Trapezium
- Java Program to calculate Area and Volume of Cube
- Java Program to calculate Area and Volume of Cuboid
- Java Program to calculate Volume of Cone