Posted on by Kalkicode
Code Geometric

Find volume of Hemisphere

A hemisphere is a three-dimensional shape that resembles half of a sphere. Calculating the volume of a hemisphere is a crucial task in various fields such as physics, engineering, and architecture, where hemispherical objects or structures are encountered. The volume of a hemisphere is used to determine the amount of space it occupies.

Problem Statement

Given the radius (r) of the hemisphere, the task is to calculate its volume. The formula for finding the volume of a hemisphere in terms of its radius 'r' is:

Volume = (2/3) * π * r³

Example Scenario

Imagine you are designing a water tank in the shape of a hemisphere for a fountain. You need to know the volume of water the tank can hold. Calculating the volume helps you determine the capacity of the tank, ensuring it meets the desired water storage requirement.

Idea to Solve the Problem

To solve this problem, you can follow these steps:

  1. Accept the radius 'r' of the hemisphere as an input.
  2. Use the formula to calculate the volume of the hemisphere.
  3. Display the calculated volume.

Pseudocode

function hemisphere_volume(r):
    volume = (2/3) * π * (r * r * r)
    return volume

main:
    r1 = 13
    r2 = 4
    r3 = 3.5
    
    volume1 = hemisphere_volume(r1)
    volume2 = hemisphere_volume(r2)
    volume3 = hemisphere_volume(r3)
    
    print("Hemisphere [ Radius :", r1, "]")
    print("Volume :", volume1)
    
    print("Hemisphere [ Radius :", r2, "]")
    print("Volume :", volume2)
    
    print("Hemisphere [ Radius :", r3, "]")
    print("Volume :", volume3)

Algorithm Explanation

  1. Define a function hemisphere_volume that takes the radius 'r' as an input.
  2. Inside the function, use the provided formula to calculate the volume of the hemisphere.
  3. In the main function, set three test cases with different radii 'r'.
  4. Calculate the volumes for each test case by calling the hemisphere_volume function.
  5. Display the calculated volumes along with their respective radius values.

Code Solution

/*
  C Program 
  Find volume of Hemisphere
*/
#include <stdio.h>

#include <math.h>

//Calculate volume of hemisphere by radius
void hemisphere_volume(double r)
{
  printf("\nGiven radius : %lf", r);
  // Formula of hemisphere volume
  //   2πr³
  //  -----
  //    3
  //Calculate volume of hemisphere
  double volume = (2 * M_PI * (r * r * r)) / 3;
  printf("\nVolume of hemisphere : %lf\n", volume);
}
int main()
{
  //Simple Case
  hemisphere_volume(13);
  hemisphere_volume(4);
  hemisphere_volume(3.5);
  return 0;
}

Output

Given radius : 13.000000
Volume of hemisphere : 4601.386040

Given radius : 4.000000
Volume of hemisphere : 134.041287

Given radius : 3.500000
Volume of hemisphere : 89.797190
// Java Program
// Find volume of Hemisphere
class Hemisphere
{
  //Calculate volume of hemisphere by radius
  public void hemisphere_volume(double r)
  {
    System.out.print("\nGiven radius : " + r);
    // Formula of hemisphere volume
    //   2πr³
    //  -----
    //    3
    //Calculate volume of hemisphere
    double volume = (2 * Math.PI * (r * r * r)) / 3;
    System.out.print("\nVolume of of hemisphere : " + volume + "\n");
  }
  public static void main(String[] args)
  {
    Hemisphere obj = new Hemisphere();
    //Simple Case
    obj.hemisphere_volume(13);
    obj.hemisphere_volume(4);
    obj.hemisphere_volume(3.5);
  }
}

Output

Given radius : 13.0
Volume of of hemisphere : 4601.386039957851

Given radius : 4.0
Volume of of hemisphere : 134.0412865531645

Given radius : 3.5
Volume of of hemisphere : 89.79719001510826
// C++ Program
// Find volume of Hemisphere
#include<iostream>
#include<math.h>
using namespace std;
class Hemisphere
{
  public:
    //Calculate volume of hemisphere by radius
    void hemisphere_volume(double r)
    {
      cout << "\nGiven radius : " << r;
      // Formula of hemisphere volume
      //   2πr³
      //  -----
      //    3
      //Calculate volume of hemisphere
      double volume = (2 * M_PI * (r * r * r)) / 3;
      cout << "\nVolume of of hemisphere : " << volume << "\n";
    }
};
int main()
{
  Hemisphere obj ;
  //Simple Case
  obj.hemisphere_volume(13);
  obj.hemisphere_volume(4);
  obj.hemisphere_volume(3.5);
  return 0;
}

Output

Given radius : 13
Volume of of hemisphere : 4601.39

Given radius : 4
Volume of of hemisphere : 134.041

Given radius : 3.5
Volume of of hemisphere : 89.7972
// C# Program
// Find volume of Hemisphere
using System;
class Hemisphere
{
  //Calculate volume of hemisphere by radius
  public void hemisphere_volume(double r)
  {
    Console.Write("\nGiven radius : " + r);
    // Formula of hemisphere volume
    //   2πr³
    //  -----
    //    3
    //Calculate volume of hemisphere
    double volume = (2 * Math.PI * (r * r * r)) / 3;
    Console.Write("\nVolume of of hemisphere : " + volume + "\n");
  }
  public static void Main(String[] args)
  {
    Hemisphere obj = new Hemisphere();
    //Simple Case
    obj.hemisphere_volume(13);
    obj.hemisphere_volume(4);
    obj.hemisphere_volume(3.5);
  }
}

Output

Given radius : 13
Volume of of hemisphere : 4601.38603995785

Given radius : 4
Volume of of hemisphere : 134.041286553164

Given radius : 3.5
Volume of of hemisphere : 89.7971900151083
<?php
// Php Program
// Find volume of Hemisphere
class Hemisphere
{
  //Calculate volume of hemisphere by radius
  public  function hemisphere_volume($r)
  {
    echo "\nGiven radius : ". $r;
    // Formula of hemisphere volume
    //   2πr³
    //  -----
    //    3
    //Calculate volume of hemisphere
    $volume = (2 * M_PI * ($r * $r * $r)) / 3;
    echo "\nVolume of of hemisphere : ". $volume ."\n";
  }
}

function main()
{
  $obj = new Hemisphere();
  //Simple Case
  $obj->hemisphere_volume(13);
  $obj->hemisphere_volume(4);
  $obj->hemisphere_volume(3.5);
}
main();

Output

Given radius : 13
Volume of of hemisphere : 4601.3860399579

Given radius : 4
Volume of of hemisphere : 134.04128655316

Given radius : 3.5
Volume of of hemisphere : 89.797190015108
// Node Js Program
// Find volume of Hemisphere
class Hemisphere
{
  //Calculate volume of hemisphere by radius
  hemisphere_volume(r)
  {
    process.stdout.write("\nGiven radius : " + r);
    // Formula of hemisphere volume
    //   2πr³
    //  -----
    //    3
    //Calculate volume of hemisphere
    var volume = (2 * Math.PI * (r * r * r)) / 3;
    process.stdout.write("\nVolume of of hemisphere : " + volume + "\n");
  }
}

function main()
{
  var obj = new Hemisphere();
  //Simple Case
  obj.hemisphere_volume(13);
  obj.hemisphere_volume(4);
  obj.hemisphere_volume(3.5);
}
main();

Output

Given radius : 13
Volume of of hemisphere : 4601.386039957851

Given radius : 4
Volume of of hemisphere : 134.0412865531645

Given radius : 3.5
Volume of of hemisphere : 89.79719001510826
#  Python 3 Program
#  Find volume of Hemisphere
import math
class Hemisphere :
  # Calculate volume of hemisphere by radius
  def hemisphere_volume(self, r) :
    print("\nGiven radius : ", r, end = "")
    #  Formula of hemisphere volume
    #    2πr³
    #   -----
    #     3
    # Calculate volume of hemisphere
    volume = (2 * math.pi * (r * r * r)) / 3
    print("\nVolume of of hemisphere : ", volume ,"\n", end = "")
  

def main() :
  obj = Hemisphere()
  # Simple Case
  obj.hemisphere_volume(13)
  obj.hemisphere_volume(4)
  obj.hemisphere_volume(3.5)

if __name__ == "__main__": main()

Output

Given radius :  13
Volume of of hemisphere :  4601.386039957851

Given radius :  4
Volume of of hemisphere :  134.0412865531645

Given radius :  3.5
Volume of of hemisphere :  89.79719001510826
#  Ruby Program
#  Find volume of Hemisphere
class Hemisphere

  # Calculate volume of hemisphere by radius
  def hemisphere_volume(r)
  
    print("\nGiven radius : ", r)
    #  Formula of hemisphere volume
    #    2πr³
    #   -----
    #     3
    # Calculate volume of hemisphere
    volume = (2 * Math::PI * (r * r * r)) / 3
    print("\nVolume of of hemisphere : ", volume ,"\n")
  end
end
def main()

  obj = Hemisphere.new()
  # Simple Case
  obj.hemisphere_volume(13)
  obj.hemisphere_volume(4)
  obj.hemisphere_volume(3.5)
end
main()

Output

Given radius : 13
Volume of of hemisphere : 4601.386039957851

Given radius : 4
Volume of of hemisphere : 134.0412865531645

Given radius : 3.5
Volume of of hemisphere : 89.79719001510826
// Scala Program
// Find volume of Hemisphere
class Hemisphere
{
  //Calculate volume of hemisphere by radius
  def hemisphere_volume(r: Double): Unit = {
    print("\nGiven radius : " + r);
    // Formula of hemisphere volume
    //   2πr³
    //  -----
    //    3
    //Calculate volume of hemisphere
    var volume: Double = (2 * Math.PI * (r * r * r)) / 3;
    print("\nVolume of of hemisphere : " + volume + "\n");
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: Hemisphere = new Hemisphere();
    //Simple Case
    obj.hemisphere_volume(13);
    obj.hemisphere_volume(4);
    obj.hemisphere_volume(3.5);
  }
}

Output

Given radius : 13.0
Volume of of hemisphere : 4601.386039957851

Given radius : 4.0
Volume of of hemisphere : 134.0412865531645

Given radius : 3.5
Volume of of hemisphere : 89.79719001510826
// Swift Program
// Find volume of Hemisphere
import Foundation
class Hemisphere
{
  //Calculate volume of hemisphere by radius
  func hemisphere_volume(_ r: Double)
  {
    print("\nGiven radius : ", r, terminator: "");
    // Formula of hemisphere volume
    //   2πr³
    //  -----
    //    3
    //Calculate volume of hemisphere
    let volume: Double = (2 * Double.pi * (r * r * r)) / 3;
    print("\nVolume of of hemisphere : ", volume ,"\n", terminator: "");
  }
}
func main()
{
  let obj: Hemisphere = Hemisphere();
  //Simple Case
  obj.hemisphere_volume(13);
  obj.hemisphere_volume(4);
  obj.hemisphere_volume(3.5);
}
main();

Output

Given radius :  13.0
Volume of of hemisphere :  4601.38603995785

Given radius :  4.0
Volume of of hemisphere :  134.041286553164

Given radius :  3.5
Volume of of hemisphere :  89.7971900151083

Output Explanation

The code calculates the volume for each test case and displays the results. For a hemisphere with a radius 'r' = 13, the volume is approximately 4601.386040. Similarly, for radii 'r' = 4 and 'r' = 3.5, the volumes are approximately 134.041287 and 89.797190, respectively.

Time Complexity

The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations (multiplication and division) and the use of the mathematical constant π (pi). These operations are computed in constant time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment