Posted on by Kalkicode
Code Geometric

Find the volume of a cylinder

A cylinder is a three-dimensional geometric shape that consists of two parallel circular bases connected by a curved surface. Finding the volume of a cylinder involves determining the amount of space enclosed within its boundaries. This calculation is crucial in various fields such as engineering, fluid dynamics, and manufacturing, where cylinders are commonly encountered.

Problem Statement

Given the radius of the circular base and the height of the cylinder, the task is to calculate its volume. The formula for finding the volume of a cylinder in terms of its radius 'r' and height 'h' is:

Volume = π * r² * h

Example Scenario

Imagine you are a civil engineer designing a water storage tank in the shape of a cylinder. To estimate the maximum volume of water the tank can hold, you need to calculate its volume. This calculation helps you plan for water supply and storage capacity.

Idea to Solve the Problem

To solve this problem, we can follow these steps:

  1. Accept the radius of the circular base and the height of the cylinder as inputs.
  2. Use the formula to calculate the volume of the cylinder.
  3. Display the calculated volume.

Pseudocode

function cylinder_volume(r, height):
    volume = π * (r * r) * height
    return volume

Algorithm Explanation

  1. Define a function cylinder_volume that takes the radius and height as inputs.
  2. Inside the function, use the provided formula to calculate the volume of the cylinder.
  3. In the main function, set four test cases with different values for the radius and height.
  4. Calculate the volumes for each test case by calling the cylinder_volume function.
  5. Display the calculated volumes along with their respective radius and height values.

Code Solution

//C Program
//Find the volume of a cylinder
#include <stdio.h>

#include <math.h>
 //Calculate volume of cylinder by given r and height
void cylinder_volume(double r, double height)
{
	// Formula
	// πr²h
	// Here h is height and r is radius
	double volume = M_PI * (r * r) * height;
	//Display result
	printf(" Cylinder Size [ r : %lf  height : %lf] ", r, height);
	printf("\n Volume : %lf\n\n", volume);
}
int main()
{
	//Test Case
	cylinder_volume(4.2, 7);
	cylinder_volume(9, 5);
	cylinder_volume(4, 6.2);
	cylinder_volume(6.7, 7.3);
	return 0;
}

Output

 Cylinder Size [ r : 4.200000  height : 7.000000]
 Volume : 387.923861

 Cylinder Size [ r : 9.000000  height : 5.000000]
 Volume : 1272.345025

 Cylinder Size [ r : 4.000000  height : 6.200000]
 Volume : 311.645991

 Cylinder Size [ r : 6.700000  height : 7.300000]
 Volume : 1029.490488
// Java Program
// Find the volume of a cylinder
class Cylinder
{
	//Calculate volume of cylinder by given r and height
	public void cylinder_volume(double r, double height)
	{
		// Formula
		// πr²h
		// Here h is height and r is radius
		double volume = Math.PI * (r * r) * height;
		//Display result
		System.out.print(" Cylinder Size [ r : " + r + " height : " + height + "] ");
		System.out.print("\n Volume : " + volume + "\n\n");
	}
	public static void main(String[] args)
	{
		Cylinder obj = new Cylinder();
		//Test Case
		obj.cylinder_volume(4.2, 7);
		obj.cylinder_volume(9, 5);
		obj.cylinder_volume(4, 6.2);
		obj.cylinder_volume(6.7, 7.3);
	}
}

Output

 Cylinder Size [ r : 4.2 height : 7.0]
 Volume : 387.92386086526767

 Cylinder Size [ r : 9.0 height : 5.0]
 Volume : 1272.345024703866

 Cylinder Size [ r : 4.0 height : 6.2]
 Volume : 311.6459912361075

 Cylinder Size [ r : 6.7 height : 7.3]
 Volume : 1029.4904878034145
// C++ Program
// Find the volume of a cylinder
#include<iostream>
#include <math.h>
using namespace std;
class Cylinder
{
	public:
		//Calculate volume of cylinder by given r and height
		void cylinder_volume(double r, double height)
		{
			// Formula
			// πr²h
			// Here h is height and r is radius
			double volume = M_PI *(r *r) *height;
			cout << " Cylinder Size [ r : " << r << " height : " << height << "] ";
			cout << "\n Volume : " << volume << "\n\n";
		}
};
int main()
{
	Cylinder obj ;
	//Test Case
	obj.cylinder_volume(4.2, 7);
	obj.cylinder_volume(9, 5);
	obj.cylinder_volume(4, 6.2);
	obj.cylinder_volume(6.7, 7.3);
	return 0;
}

Output

 Cylinder Size [ r : 4.2 height : 7]
 Volume : 387.924

 Cylinder Size [ r : 9 height : 5]
 Volume : 1272.35

 Cylinder Size [ r : 4 height : 6.2]
 Volume : 311.646

 Cylinder Size [ r : 6.7 height : 7.3]
 Volume : 1029.49
// C# Program
// Find the volume of a cylinder
using System;
class Cylinder
{
	//Calculate volume of cylinder by given r and height
	public void cylinder_volume(double r, double height)
	{
		// Formula
		// πr²h
		// Here h is height and r is radius
		double volume = Math.PI * (r * r) * height;
		Console.Write(" Cylinder Size [ r : " + r + " height : " + height + "] ");
		Console.Write("\n Volume : " + volume + "\n\n");
	}
	public static void Main(String[] args)
	{
		Cylinder obj = new Cylinder();
		//Test Case
		obj.cylinder_volume(4.2, 7);
		obj.cylinder_volume(9, 5);
		obj.cylinder_volume(4, 6.2);
		obj.cylinder_volume(6.7, 7.3);
	}
}

Output

 Cylinder Size [ r : 4.2 height : 7]
 Volume : 387.923860865268

 Cylinder Size [ r : 9 height : 5]
 Volume : 1272.34502470387

 Cylinder Size [ r : 4 height : 6.2]
 Volume : 311.645991236107

 Cylinder Size [ r : 6.7 height : 7.3]
 Volume : 1029.49048780341
<?php
// Php Program
// Find the volume of a cylinder
class Cylinder
{
	//Calculate volume of cylinder by given r and height
	public 	function cylinder_volume($r, $height)
	{
		// Formula
		// πr²h
		// Here h is height and r is radius
		$volume = M_PI *($r *$r) *$height;
		//Display result
		echo(" Cylinder Size [ r : ". $r ." height : ". $height ."] ");
		echo("\n Volume : ". $volume ."\n\n");
	}
}

function main()
{
	$obj = new Cylinder();
	//Test Case
	$obj->cylinder_volume(4.2, 7);
	$obj->cylinder_volume(9, 5);
	$obj->cylinder_volume(4, 6.2);
	$obj->cylinder_volume(6.7, 7.3);
}
main();

Output

 Cylinder Size [ r : 4.2 height : 7]
 Volume : 387.92386086527

 Cylinder Size [ r : 9 height : 5]
 Volume : 1272.3450247039

 Cylinder Size [ r : 4 height : 6.2]
 Volume : 311.64599123611

 Cylinder Size [ r : 6.7 height : 7.3]
 Volume : 1029.4904878034
// Node Js Program
// Find the volume of a cylinder
class Cylinder
{
	//Calculate volume of cylinder by given r and height
	cylinder_volume(r, height)
	{
		// Formula
		// πr²h
		// Here h is height and r is radius
		var volume = Math.PI *(r *r) *height;
		//Display result
		process.stdout.write(" Cylinder Size [ r : " + r + " height : " + height + "] ");
		process.stdout.write("\n Volume : " + volume + "\n\n");
	}
}

function main(args)
{
	var obj = new Cylinder();
	//Test Case
	obj.cylinder_volume(4.2, 7);
	obj.cylinder_volume(9, 5);
	obj.cylinder_volume(4, 6.2);
	obj.cylinder_volume(6.7, 7.3);
}
main();

Output

 Cylinder Size [ r : 4.2 height : 7]
 Volume : 387.92386086526767

 Cylinder Size [ r : 9 height : 5]
 Volume : 1272.345024703866

 Cylinder Size [ r : 4 height : 6.2]
 Volume : 311.6459912361075

 Cylinder Size [ r : 6.7 height : 7.3]
 Volume : 1029.4904878034145
#  Python 3 Program
#  Find the volume of a cylinder
import math
class Cylinder :
	# Calculate volume of cylinder by given r and height
	def cylinder_volume(self, r, height) :
		#  Formula
		#  πr²h
		#  Here h is height and r is radius
		volume = math.pi * (r * r) * height
		# Display result
		print(" Cylinder Size [ r : ", r ," height : ", height ,"] ")
		print(" Volume : ", volume ,"\n")
	

def main() :
	obj = Cylinder()
	# Test Case
	obj.cylinder_volume(4.2, 7)
	obj.cylinder_volume(9, 5)
	obj.cylinder_volume(4, 6.2)
	obj.cylinder_volume(6.7, 7.3)


if __name__ == "__main__": main()

Output

 Cylinder Size [ r :  4.2  height :  7 ]
 Volume :  387.92386086526767

 Cylinder Size [ r :  9  height :  5 ]
 Volume :  1272.345024703866

 Cylinder Size [ r :  4  height :  6.2 ]
 Volume :  311.6459912361075

 Cylinder Size [ r :  6.7  height :  7.3 ]
 Volume :  1029.4904878034145
#  Ruby Program
#  Find the volume of a cylinder
class Cylinder

	# Calculate volume of cylinder by given r and height
	def cylinder_volume(r, height)
	
		#  Formula
		#  πr²h
		#  Here h is height and r is radius
		volume = Math::PI * (r * r) * height
		# Display result
		print(" Cylinder Size [ r  : ", r ," height  : ", height ,"] ")
		print("\n Volume  : ", volume ,"\n\n")
	end
end
def main()

	obj = Cylinder.new()
	# Test Case
	obj.cylinder_volume(4.2, 7)
	obj.cylinder_volume(9, 5)
	obj.cylinder_volume(4, 6.2)
	obj.cylinder_volume(6.7, 7.3)
end
main()

Output

 Cylinder Size [ r  : 4.2 height  : 7] 
 Volume  : 387.92386086526767

 Cylinder Size [ r  : 9 height  : 5] 
 Volume  : 1272.345024703866

 Cylinder Size [ r  : 4 height  : 6.2] 
 Volume  : 311.6459912361075

 Cylinder Size [ r  : 6.7 height  : 7.3] 
 Volume  : 1029.4904878034145

// Scala Program
// Find the volume of a cylinder
class Cylinder
{
	//Calculate volume of cylinder by given r and height
	def cylinder_volume(r: Double, height: Double): Unit = {
		// Formula
		// πr²h
		// Here h is height and r is radius
		var volume: Double = Math.PI * (r * r) * height;
		//Display result
		print(" Cylinder Size [ r : " + r + " height : " + height + "] ");
		print("\n Volume : " + volume + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: Cylinder = new Cylinder();
		//Test Case
		obj.cylinder_volume(4.2, 7);
		obj.cylinder_volume(9, 5);
		obj.cylinder_volume(4, 6.2);
		obj.cylinder_volume(6.7, 7.3);
	}
}

Output

 Cylinder Size [ r : 4.2 height : 7.0]
 Volume : 387.92386086526767

 Cylinder Size [ r : 9.0 height : 5.0]
 Volume : 1272.345024703866

 Cylinder Size [ r : 4.0 height : 6.2]
 Volume : 311.6459912361075

 Cylinder Size [ r : 6.7 height : 7.3]
 Volume : 1029.4904878034145
// Swift Program
// Find the volume of a cylinder

class Cylinder
{
	//Calculate volume of cylinder by given r and height
	func cylinder_volume(_ r: Double, _ height: Double)
	{
		// Formula
		// πr²h
		// Here h is height and r is radius
		let volume: Double = Double.pi * (r * r) * height;
		//Display result
		print(" Cylinder Size [ r : ", r ," height : ", height ,"] ", terminator: "");
		print("\n Volume : ", volume ,"\n\n", terminator: "");
	}
}
func main()
{
	let obj: Cylinder = Cylinder();
	//Test Case
	obj.cylinder_volume(4.2, 7);
	obj.cylinder_volume(9, 5);
	obj.cylinder_volume(4, 6.2);
	obj.cylinder_volume(6.7, 7.3);
}
main();

Output

 Cylinder Size [ r :  4.2  height :  7.0 ]
 Volume :  387.923860865268

 Cylinder Size [ r :  9.0  height :  5.0 ]
 Volume :  1272.34502470387

 Cylinder Size [ r :  4.0  height :  6.2 ]
 Volume :  311.645991236107

 Cylinder Size [ r :  6.7  height :  7.3 ]
 Volume :  1029.49048780341

Output Explanation

The code calculates the volume for each test case and displays the results. For a cylinder with a radius of 4.2 and a height of 7, the volume is approximately 387.923861. Similarly, for radius 9 and height 5, the volume is approximately 1272.345025. 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.

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