# Find the volume of a cone

A cone is a three-dimensional geometric shape that has a circular base and a pointed top. Finding the volume of a cone involves calculating the amount of space enclosed within its boundaries. This calculation is important in various fields such as engineering, architecture, and fluid dynamics, where cone-like structures and containers are encountered.

## Problem Statement

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

Volume = (1/3) * π * r² * h

## Example Scenario

Imagine you are a chef preparing a dessert in the shape of a cone. To determine how much ice cream or any other ingredient you need to fill the cone, you need to calculate its volume. This calculation helps you plan for the right amount of ingredients to create the perfect dessert.

## 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 cone as inputs.
2. Use the formula to calculate the volume of the cone.
3. Display the calculated volume.

## Pseudocode

``````function cone_volume(r, h):
volume = (1/3) * π * (r * r) * h
return volume
``````

## Algorithm Explanation

1. Define a function `cone_volume` that takes the radius and height as inputs.
2. Inside the function, use the provided formula to calculate the volume of the cone.
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 `cone_volume` function.
5. Display the calculated volumes along with their respective radius and height values.

## Code Solution

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

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

#### Output

`````` Cone Size [ r : 5.000000  h : 3.000000]
Volume : 78.539816

Cone Size [ r : 9.000000  h : 5.000000]
Volume : 424.115008

Cone Size [ r : 4.000000  h : 6.200000]
Volume : 103.881997

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

#### Output

`````` Cone Size [ r : 5.0 h : 3.0]
Volume : 78.53981633974483

Cone Size [ r : 9.0 h : 5.0]
Volume : 424.11500823462205

Cone Size [ r : 4.0 h : 6.2]
Volume : 103.8819970787025

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

#### Output

`````` Cone Size [ r : 5 h : 3]
Volume : 78.5398

Cone Size [ r : 9 h : 5]
Volume : 424.115

Cone Size [ r : 4 h : 6.2]
Volume : 103.882

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

#### Output

`````` Cone Size [ r : 5 h : 3]
Volume : 78.5398163397448

Cone Size [ r : 9 h : 5]
Volume : 424.115008234622

Cone Size [ r : 4 h : 6.2]
Volume : 103.881997078702

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

function main()
{
\$obj = new Cone();
//Test Case
\$obj->cone_volume(5, 3);
\$obj->cone_volume(9, 5);
\$obj->cone_volume(4, 6.2);
\$obj->cone_volume(6.7, 7.3);
}
main();``````

#### Output

`````` Cone Size [ r : 5 h : 3]
Volume : 78.539816339745

Cone Size [ r : 9 h : 5]
Volume : 424.11500823462

Cone Size [ r : 4 h : 6.2]
Volume : 103.8819970787

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

function main(args)
{
var obj = new Cone();
//Test Case
obj.cone_volume(5, 3);
obj.cone_volume(9, 5);
obj.cone_volume(4, 6.2);
obj.cone_volume(6.7, 7.3);
}
main();``````

#### Output

`````` Cone Size [ r : 5 h : 3]
Volume : 78.53981633974483

Cone Size [ r : 9 h : 5]
Volume : 424.11500823462205

Cone Size [ r : 4 h : 6.2]
Volume : 103.8819970787025

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

def main() :
obj = Cone()
# Test Case
obj.cone_volume(5, 3)
obj.cone_volume(9, 5)
obj.cone_volume(4, 6.2)
obj.cone_volume(6.7, 7.3)

if __name__ == "__main__": main()``````

#### Output

`````` Cone Size [ r :  5  h :  3 ]
Volume :  78.53981633974483

Cone Size [ r :  9  h :  5 ]
Volume :  424.11500823462205

Cone Size [ r :  4  h :  6.2 ]
Volume :  103.8819970787025

Cone Size [ r :  6.7  h :  7.3 ]
Volume :  343.1634959344715
``````
``````#  Ruby Program
#  Find the volume of a cone
class Cone

# Calculate volume of cone by given r and height
def cone_volume(r, h)

#  Formula
#  πr²h/3
#  Here h is height and r is radius
volume = Math::PI * (r * r) * h / 3
# Display result
print(" Cone Size [ r  : ", r ," h  : ", h ,"] ")
print("\n Volume  : ", volume ,"\n\n")
end
end
def main()

obj = Cone.new()
# Test Case
obj.cone_volume(5, 3)
obj.cone_volume(9, 5)
obj.cone_volume(4, 6.2)
obj.cone_volume(6.7, 7.3)
end
main()``````

#### Output

`````` Cone Size [ r  : 5 h  : 3]
Volume  : 78.53981633974483

Cone Size [ r  : 9 h  : 5]
Volume  : 424.11500823462205

Cone Size [ r  : 4 h  : 6.2]
Volume  : 103.8819970787025

Cone Size [ r  : 6.7 h  : 7.3]
Volume  : 343.1634959344715

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

#### Output

`````` Cone Size [ r : 5.0 h : 3.0]
Volume : 78.53981633974483

Cone Size [ r : 9.0 h : 5.0]
Volume : 424.11500823462205

Cone Size [ r : 4.0 h : 6.2]
Volume : 103.8819970787025

Cone Size [ r : 6.7 h : 7.3]
Volume : 343.1634959344715
``````
``````// Swift Program
// Find the volume of a cone
class Cone
{
//Calculate volume of cone by given r and height
func cone_volume(_ r: Double, _ h: Double)
{
// Formula
// πr²h/3
// Here h is height and r is radius
let volume: Double = Double.pi * (r * r) * h / 3;
//Display result
print(" Cone Size [ r : ", r ," h : ", h ,"] ");
print(" Volume : ", volume ,"\n");
}
}
func main()
{
let obj: Cone = Cone();
//Test Case
obj.cone_volume(5, 3);
obj.cone_volume(9, 5);
obj.cone_volume(4, 6.2);
obj.cone_volume(6.7, 7.3);
}
main();``````

#### Output

`````` Cone Size [ r :  5.0  h :  3.0 ]
Volume :  78.5398163397448

Cone Size [ r :  9.0  h :  5.0 ]
Volume :  424.115008234622

Cone Size [ r :  4.0  h :  6.2 ]
Volume :  103.881997078702

Cone Size [ r :  6.7  h :  7.3 ]
Volume :  343.163495934471
``````

## Output Explanation

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