In the previous article we have discussed Java Program to Find Area and Circumference of Circle
In this article we will discuss about how to find volume and surface area of sphere.
Program to Find Volume and Surface Area of Sphere
Before jumping into the program directly, let’s first know how can we get volume and surface area of sphere.
Formula for Volume of Sphere = (4/3)*pie*(radius*radius*radius)
Formula for Surface Area of Sphere = 4*pie*(radius*radius)
Where,
'pie'
represents PI value i.e. 3.141'radius'
represents radius of sphere.
Example:
Example- To find Volume of Sphere When radius of sphere = 1 Then volume of sphere => area = (4/3)*pie*(radius*radius*radius) => area = 4.1887902047863905
Example- To find Surface Area of Sphere When radius of sphere = 1 Then surface area of sphere => Surface Area = 4*pie*(radius*radius) => Surface Area = 12.566370614359172
Now, let’s see the program.
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Method-1: Java Program to Find Volume and Surface Area of Sphere By Using Static Values
In this, the radius value of the sphere is already declared by the program.
Let’s see the program to know how it is actually implemented.
import java.util.*; public class Main { public static void main(String[] args) { //radius of sphere declared double radius=1; //finding surface area of sphere double surfaceArea = 4 * Math.PI * radius * radius; //finding volume of sphere double volume = (4.0 / 3) * Math.PI * radius * radius * radius; System.out.println("Surface area of Sphere = "+surfaceArea); System.out.println("Volume of Sphere = "+ volume); } }
Output: Surface area of Sphere = 12.566370614359172 Volume of Sphere = 4.1887902047863905
Method-2: Java Program to Find Volume and Surface Area of Sphere By Using User Input Values
In this, the radius value of the sphere will be taken as input from the user.
Let’s see the program to know how it is actually implemented.
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); double radius, surfaceArea, volume; System.out.print("Enter the radius of Sphere = "); radius = sc.nextDouble(); surfaceArea = 4 * Math.PI * radius * radius; volume = (4.0 / 3) * Math.PI * radius * radius * radius; System.out.println("Surface area of Sphere = "+surfaceArea); System.out.println("Volume of Sphere = "+ volume); } }
Output: Enter the radius of Sphere = 1 Surface area of Sphere = 12.566370614359172 Volume of Sphere = 4.1887902047863905
Method-3: Java Program to Find Volume and Surface Area of Sphere By Using User Defined Method
In this, the radius value of the sphere will be taken as input from the user. And that radius value will be passed as the parameter to the user defined method.
Let’s see the program to know how it is actually implemented.
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); double radius; //taking radius input from user System.out.print("Please Enter the radius of a Sphere : "); radius = sc.nextDouble(); //calling the calulate() method calculate(radius); } //user defined method i.e calculate() method // to find surface area and volume of sphere public static void calculate(double radius) { //finding surface area of sphere double surfaceArea = 4 * Math.PI * radius * radius; //finding volume of sphere double volume = (4.0 / 3) * Math.PI * radius * radius * radius; System.out.println("Surface area of Sphere = "+surfaceArea); System.out.println("Volume of Sphere = "+ volume); } }
Output: Enter the radius of Sphere = 1 Surface area of Sphere = 12.566370614359172 Volume of Sphere = 4.1887902047863905
Are you a job seeker and trying to find simple java programs for Interview? This would be the right
choice for you, just tap on the link and start preparing the java programs covered to crack the
interview.
Related Java Programs: