How to find volume of hemisphere: In the previous article, we have seen Java Program to Find Surface Area of Octahedron
In this article we are going to see how to find the volume of hemisphere using Java programming language.
Java Program to Find Volume of Hemisphere
Find volume of hemisphere: Before Jumping into the program directly let’s see how we can find the volume of hemisphere.
Explanation:
Formula to find volume of hemisphere: (2/3) * pie * (radius of hemisphere) ^3
Example:
Let radius of hemisphere be “r” = 1 & Pie = 3.14 So, vol. of hemisphere = 2/3 * pie* r^3 => 2/3 * 3.14 * 1^3 = 2.09
Let’s see different ways to find volume of hemisphere.
Method-1: Java Program to Find Volume of Hemisphere By Using Static Value
Approach:
- Declare an integer variable say ‘
r
’ and assign the value to it. ‘r
‘ holds the radius value of hemisphere. - Declare the value of pie i.e 3.14
- Find the volume of hemisphere using the formula
(2/3) * pie * (radius of hemisphere) ^3
- Print the result.
Program:
class Main { public static void main(String[] args) { //declaring 'r' value int r = 1; //declaring pie value double pie = 3.14; //find volume of hemisphere by using formula double vol = (2 * pie * r * r * r)/3; System.out.println("The volume of hemisphere is: " + vol); } }
Output: The volume of hemisphere is: 2.0933333333333333
Method-2: Java Program to Find Volume of Hemisphere By Using User Input Value
Approach:
- Declare an integer variable say ‘
r
’ and take the value of it as user input. ‘r
‘ holds the radius value of hemisphere. - Declare the value of pie i.e 3.14
- Find the volume of hemisphere using the formula
(2/3) * pie * (radius of hemisphere) ^3
- Print the result.
Program:
import java.util.*; class Main { public static void main(String[] args) { //Creating the object of Scanner class Scanner s = new Scanner(System.in); //taking input of radius value of hemisphere System.out.println("Enter the value of radius of the hemisphere: "); int r = s.nextInt(); //declaring pie value double pie = 3.14; //find volume of hemisphere by using formula double vol = (2 * pie * r * r * r)/3; System.out.println("The volume of hemisphere is: " + vol); } }
Output: Enter the value of radius of the hemisphere: 5 The volume of hemisphere is: 261.6666666666667
Method-3: Java Program to Find Volume of Hemisphere By Using User Defined Method
Approach:
- Declare an integer variable say ‘
r
’ and take the value of it as user input. ‘r
‘ holds the radius value of hemisphere. - Call
findVol()
method by passing the radius as parameter. - Inside method, declare the value of pie i.e 3.14
- Then find the volume of hemisphere using the formula
(2/3) * pie * (radius of hemisphere) ^3
- Print the result.
Program:
import java.util.*; class Main { public static void main(String[] args) { //Creating the object of Scanner class Scanner s = new Scanner(System.in); //taking input of radius value of hemisphere System.out.println("Enter the value of radius of the hemisphere: "); int r = s.nextInt(); //calling findVol() method findVol(r); } //findVol() method to find volume of hemisphere public static void findVol(int r) { //declaring pie value double pie = 3.14; //find volume of hemisphere by using formula double vol = (2 * pie * r * r * r)/3; System.out.println("The volume of hemisphere is: " + vol); } }
Output: Enter the value of radius of the hemisphere: 4 The volume of hemisphere is: 133.97333333333333
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output
Related Java Articles: