Find the volume of a sphere

Here given code implementation process.

//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


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







© 2021, kalkicode.com, All rights reserved