Find the volume of a sphere
A sphere is a three-dimensional geometric object that is perfectly round and symmetric. Finding the volume of a sphere involves calculating the amount of space enclosed within its boundaries. This calculation is important in various fields such as physics, astronomy, and engineering, where spheres are used to represent celestial bodies, particles, and geometric shapes.
Problem Statement
Given the radius of the sphere, the task is to calculate its volume. The formula for finding the volume of a sphere in terms of its radius 'r' is:
Volume = (4/3) * π * r³
Example Scenario
Imagine you are an astronomer studying planets. To determine the internal composition and density of a planet, you need to calculate its volume. This calculation helps you understand the planet's overall size and its potential to support various atmospheric and geological phenomena.
Idea to Solve the Problem
To solve this problem, we can follow these steps:
- Accept the radius of the sphere as input.
- Check if the input radius is non-negative.
- Use the formula to calculate the volume of the sphere.
- Display the calculated volume.
Pseudocode
function sphere_volume(r):
if r < 0:
return
volume = (4/3) * π * (r * r * r)
return volume
main:
radius1 = 5
radius2 = 9
radius3 = 4.3
radius4 = 6.7
volume1 = sphere_volume(radius1)
volume2 = sphere_volume(radius2)
volume3 = sphere_volume(radius3)
volume4 = sphere_volume(radius4)
print("Sphere Size [ r :", radius1, "]")
print("Volume :", volume1)
print("Sphere Size [ r :", radius2, "]")
print("Volume :", volume2)
print("Sphere Size [ r :", radius3, "]")
print("Volume :", volume3)
print("Sphere Size [ r :", radius4, "]")
print("Volume :", volume4)
Algorithm Explanation
- Define a function
sphere_volume
that takes the radius as input. - Inside the function, check if the input radius is non-negative. If it's negative, return without calculating the volume.
- If the radius is non-negative, use the provided formula to calculate the volume of the sphere.
- In the
main
function, set four test cases with different values for the radius. - Calculate the volumes for each test case by calling the
sphere_volume
function. - Display the calculated volumes along with their respective radius values.
Code Solution
//C Program
//Find the volume of a sphere
#include <stdio.h>
#include <math.h>
//Calculate volume of sphere by given radius
void sphere_volume(double r)
{
if (r < 0.0)
{
return;
}
// Formula
// 4
// -- πr³
// 3
// Here r is radius
double volume = 4 * M_PI * (r * r * r) / 3;
//Display result
printf(" Sphere Size [ r : %lf ] ", r);
printf("\n Volume : %lf\n\n", volume);
}
int main()
{
//Test Case
sphere_volume(5);
sphere_volume(9);
sphere_volume(4.3);
sphere_volume(6.7);
return 0;
}
Output
Sphere Size [ r : 5.000000 ]
Volume : 523.598776
Sphere Size [ r : 9.000000 ]
Volume : 3053.628059
Sphere Size [ r : 4.300000 ]
Volume : 333.038143
Sphere Size [ r : 6.700000 ]
Volume : 1259.833108
// Java Program
// Find the volume of a sphere
class Sphere
{
//Calculate volume of sphere by given radius
public void sphere_volume(double r)
{
if (r < 0.0)
{
return;
}
// Formula
// 4
// -- πr³
// 3
// Here r is radius
double volume = 4 * Math.PI * (r * r * r) / 3;
//Display result
System.out.print(" Sphere Size [ r : " + r + " ] ");
System.out.print("\n Volume : " + volume + "\n\n");
}
public static void main(String[] args)
{
Sphere obj = new Sphere();
//Test Case
obj.sphere_volume(5);
obj.sphere_volume(9);
obj.sphere_volume(4.3);
obj.sphere_volume(6.7);
}
}
Output
Sphere Size [ r : 5.0 ]
Volume : 523.5987755982989
Sphere Size [ r : 9.0 ]
Volume : 3053.6280592892786
Sphere Size [ r : 4.3 ]
Volume : 333.03814281195156
Sphere Size [ r : 6.7 ]
Volume : 1259.8331083621695
// C++ Program
// Find the volume of a sphere
#include<iostream>
#include <math.h>
using namespace std;
class Sphere
{
public:
//Calculate volume of sphere by given radius
void sphere_volume(double r)
{
if (r < 0.0)
{
return;
}
// Formula
// 4
// -- πr³
// 3
// Here r is radius
double volume = 4 * M_PI *(r *r *r) / 3;
cout << " Sphere Size [ r : " << r << " ] ";
cout << "\n Volume : " << volume << "\n\n";
}
};
int main()
{
Sphere obj ;
//Test Case
obj.sphere_volume(5);
obj.sphere_volume(9);
obj.sphere_volume(4.3);
obj.sphere_volume(6.7);
return 0;
}
Output
Sphere Size [ r : 5 ]
Volume : 523.599
Sphere Size [ r : 9 ]
Volume : 3053.63
Sphere Size [ r : 4.3 ]
Volume : 333.038
Sphere Size [ r : 6.7 ]
Volume : 1259.83
// C# Program
// Find the volume of a sphere
using System;
class Sphere
{
//Calculate volume of sphere by given radius
public void sphere_volume(double r)
{
if (r < 0.0)
{
return;
}
// Formula
// 4
// -- πr³
// 3
// Here r is radius
double volume = 4 * Math.PI * (r * r * r) / 3;
Console.Write(" Sphere Size [ r : " + r + " ] ");
Console.Write("\n Volume : " + volume + "\n\n");
}
public static void Main(String[] args)
{
Sphere obj = new Sphere();
//Test Case
obj.sphere_volume(5);
obj.sphere_volume(9);
obj.sphere_volume(4.3);
obj.sphere_volume(6.7);
}
}
Output
Sphere Size [ r : 5 ]
Volume : 523.598775598299
Sphere Size [ r : 9 ]
Volume : 3053.62805928928
Sphere Size [ r : 4.3 ]
Volume : 333.038142811952
Sphere Size [ r : 6.7 ]
Volume : 1259.83310836217
<?php
// Php Program
// Find the volume of a sphere
class Sphere
{
//Calculate volume of sphere by given radius
public function sphere_volume($r)
{
if ($r < 0.0)
{
return;
}
// Formula
// 4
// -- πr³
// 3
// Here r is radius
$volume = 4 * M_PI *($r *$r *$r) / 3;
//Display result
echo(" Sphere Size [ r : ". $r ." ] ");
echo("\n Volume : ". $volume ."\n\n");
}
}
function main()
{
$obj = new Sphere();
//Test Case
$obj->sphere_volume(5);
$obj->sphere_volume(9);
$obj->sphere_volume(4.3);
$obj->sphere_volume(6.7);
}
main();
Output
Sphere Size [ r : 5 ]
Volume : 523.5987755983
Sphere Size [ r : 9 ]
Volume : 3053.6280592893
Sphere Size [ r : 4.3 ]
Volume : 333.03814281195
Sphere Size [ r : 6.7 ]
Volume : 1259.8331083622
// Node Js Program
// Find the volume of a sphere
class Sphere
{
//Calculate volume of sphere by given radius
sphere_volume(r)
{
if (r < 0.0)
{
return;
}
// Formula
// 4
// -- πr³
// 3
// Here r is radius
var volume = 4 *Math.PI *(r *r *r) / 3;
//Display result
process.stdout.write(" Sphere Size [ r : " + r + " ] ");
process.stdout.write("\n Volume : " + volume + "\n\n");
}
}
function main(args)
{
var obj = new Sphere();
//Test Case
obj.sphere_volume(5);
obj.sphere_volume(9);
obj.sphere_volume(4.3);
obj.sphere_volume(6.7);
}
main();
Output
Sphere Size [ r : 5 ]
Volume : 523.5987755982989
Sphere Size [ r : 9 ]
Volume : 3053.6280592892786
Sphere Size [ r : 4.3 ]
Volume : 333.03814281195156
Sphere Size [ r : 6.7 ]
Volume : 1259.8331083621695
# Python 3 Program
# Find the volume of a sphere
import math
class Sphere :
# Calculate volume of sphere by given radius
def sphere_volume(self, r) :
if (r < 0.0) :
return
# Formula
# 4
# -- πr³
# 3
# Here r is radius
volume = int(4 * math.pi * (r * r * r) / 3)
# Display result
print(" Sphere Size [ r : ", r ," ] ", end = "")
print("\n Volume : ", volume ,"\n\n", end = "")
def main() :
obj = Sphere()
# Test Case
obj.sphere_volume(5)
obj.sphere_volume(9)
obj.sphere_volume(4.3)
obj.sphere_volume(6.7)
if __name__ == "__main__": main()
Output
Sphere Size [ r : 5 ]
Volume : 523
Sphere Size [ r : 9 ]
Volume : 3053
Sphere Size [ r : 4.3 ]
Volume : 333
Sphere Size [ r : 6.7 ]
Volume : 1259
# Ruby Program
# Find the volume of a sphere
class Sphere
# Calculate volume of sphere by given radius
def sphere_volume(r)
if (r < 0.0)
return
end
# Formula
# 4
# -- πr³
# 3
# Here r is radius
volume = 4 * Math::PI * (r * r * r) / 3
# Display result
print(" Sphere Size [ r :", r ," ] ")
print("\n Volume :", volume ,"\n\n")
end
end
def main()
obj = Sphere.new()
# Test Case
obj.sphere_volume(5)
obj.sphere_volume(9)
obj.sphere_volume(4.3)
obj.sphere_volume(6.7)
end
main()
Output
Sphere Size [ r :5 ]
Volume :523.5987755982989
Sphere Size [ r :9 ]
Volume :3053.6280592892786
Sphere Size [ r :4.3 ]
Volume :333.03814281195156
Sphere Size [ r :6.7 ]
Volume :1259.8331083621695
// Scala Program
// Find the volume of a sphere
class Sphere
{
//Calculate volume of sphere by given radius
def sphere_volume(r: Double): Unit = {
if (r < 0.0)
{
return;
}
// Formula
// 4
// -- πr³
// 3
// Here r is radius
var volume: Double = 4 * Math.PI * (r * r * r) / 3;
//Display result
print(" Sphere Size [ r : " + r + " ] ");
print("\n Volume : " + volume + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Sphere = new Sphere();
//Test Case
obj.sphere_volume(5);
obj.sphere_volume(9);
obj.sphere_volume(4.3);
obj.sphere_volume(6.7);
}
}
Output
Sphere Size [ r : 5.0 ]
Volume : 523.5987755982989
Sphere Size [ r : 9.0 ]
Volume : 3053.6280592892786
Sphere Size [ r : 4.3 ]
Volume : 333.03814281195156
Sphere Size [ r : 6.7 ]
Volume : 1259.8331083621695
// Swift Program
// Find the volume of a sphere
class Sphere
{
//Calculate volume of sphere by given radius
func sphere_volume(_ r: Double)
{
if (r < 0.0)
{
return;
}
// Formula
// 4
// -- πr³
// 3
// Here r is radius
let volume: Double = 4 * Double.pi * (r * r * r) / 3;
//Display result
print(" Sphere Size [ r : ", r ," ] ");
print(" Volume : ", volume ,"\n");
}
}
func main()
{
let obj: Sphere = Sphere();
//Test Case
obj.sphere_volume(5);
obj.sphere_volume(9);
obj.sphere_volume(4.3);
obj.sphere_volume(6.7);
}
main();
Output
Sphere Size [ r : 5.0 ]
Volume : 523.598775598299
Sphere Size [ r : 9.0 ]
Volume : 3053.62805928928
Sphere Size [ r : 4.3 ]
Volume : 333.038142811952
Sphere Size [ r : 6.7 ]
Volume : 1259.83310836217
Output Explanation
The code calculates the volume for each test case and displays the results. For a sphere with a radius of 5, the volume is approximately 523.598776. Similarly, for radius 9, the volume is approximately 3053.628059. The third and fourth test cases follow the same pattern.
Time Complexity
The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations and the value of π, which are calculated in constant time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.
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