find the volume of a cone in swift

To find the volume of a cone, we need to know its radius and height. The formula to calculate the volume of a cone is:

main.swift
V = (1/3) x π x r^2 x h
24 chars
2 lines

Where r is the radius of the base of the cone and h is the height of the cone. π is a mathematical constant that represents the ratio of the circumference of a circle to its diameter and its value is approximately 3.14159.

Here's the Swift code to calculate the volume of a cone:

main.swift
func coneVolume(radius: Double, height: Double) -> Double {
  let pi = 3.14159
  let volume = (1.0/3.0) * pi * pow(radius, 2) * height
  return volume
}
153 chars
6 lines

To use this function, you can call it with the values of the cone's radius and height:

main.swift
let coneRadius = 4.0
let coneHeight = 8.0
let volume = coneVolume(radius: coneRadius, height: coneHeight)
print("The volume of the cone is: \(volume)")
152 chars
5 lines

This will output:

main.swift
The volume of the cone is: 134.0410133333333
45 chars
2 lines

related categories

gistlibby LogSnag