# Java Program to Find Volume and Surface Area of Sphere

This is the Java Program to Find the Surface Area and Volume of a Sphere.

Problem Description

Given the dimensions of a sphere, find out its surface area and volume.

Problem Solution

The surface area and volume of a sphere can be calculated using the formulas:

Surface Area = 4 * PI * (radius3) /3.
Volume = 4 * PI * (radius2).

Program/Source Code

Here is the source code of the Java Program to Find the Surface Area and Volume of a Sphere. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

1. ` `
2. `//Java Program to Find the Surface Area and Volume of a Sphere`
3. ` `
4. `import java.io.BufferedReader;`
5. `import java.io.InputStreamReader;`
6. ` `
7. `public class Sphere {`
8. `    // Function to calculate and print the surface area and volume of a sphere`
9. `    public static void main(String[] args) {`
10. `        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));`
11. `        double radius;`
12. `        System.out.println("Enter the radius of the sphere");`
13. `        try{`
14. `            radius=Double.parseDouble(br.readLine());`
15. `        }catch (Exception e){`
16. `            System.out.println("An error occurred");`
17. `            return;`
18. `        }`
19. `        if(radius<=0){`
20. `            System.out.println("Wrong Input");`
21. `            return;`
22. `        }`
23. `        System.out.println("Volume = " + (4*Math.pow(radius,3)*Math.PI)/3);`
24. `        System.out.println("Surface area = " + (4*Math.PI*Math.pow(radius,2)));`
25. `    }`
26. `}`
Program Explanation

1. In function main(), first, the radius of the sphere is taken as input.
2. The condition if(radius<=0) checks if the input is valid or not.
3. The print statement displays the surface area and volume of the sphere.

Time Complexity: O(1)

Runtime Test Cases
```
Case 1 (Simple Test Case):

Enter the radius of the sphere
4.55
Volume = 394.56885292638566
Surface area = 260.15528764377075```

Sanfoundry Global Education & Learning Series – Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]