
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Volume of Cube in Swift
This tutorial will discuss how to write a Swift program to find the volume of cube.
A cube is a solid three-dimensional shape with six square faces and all the sides of the cube are of equal length(that means Length = Breadth = Width). It is also known as a regular hexahedron. It contains 12 edges and 8 vertices.
The total number of cubic units contained by the cube is known as the volume of the cube. To calculate the volume of the cube we find the product of length, height, and width. As we know that length = height = width, hence the volume of the cube is x * x * x. Here x is the edge length.
Formula
Following is the formula of the volume of the cube ?
Volume = (side)3
Below is a demonstration of the same ?
Input
Suppose our given input is ?
side = 5
Output
The desired output would be ?
Volume = 5 * 5 * 5 = 125. Volume = 125
Algorithm
Following is the algorithm ?
Step 1 ? Declare a variable with user-defined/pre-defined value. This variable stores the side of the cube.
Step 2 ? Declare another variable named "cubeVolume" to store the volume of the cube. var cubeVolume = cubeSide * cubeSide * cubeSide
Step 3 ? Print the output.
Example 1
The following program shows how to find the volume of the cube.
import Foundation import Glibc var cubeSide : Int = 4 // Finding the volume of the cube var cubeVolume = cubeSide * cubeSide * cubeSide print("Side - ", cubeSide) print("So, Volume of the cube- ", cubeVolume)
Output
Side - 4 So, Volume of the cube- 64
Here, in the above code, we find the volume of the cube using the following formula ?
var cubeVolume = cubeSide * cubeSide * cubeSide
And display the volume of the cube that is 64(V = 4 * 4 * 4 = 64).
Example 2
The following program shows how to find the volume of the cube with user-defined input.
import Foundation import Glibc print("Please enter the side") var cubeSide = Double(readLine()!)! // Finding the volume of the cube var cubeVolume = pow(cubeSide, 3) print("Side - ", cubeSide) print("So, Volume of the cube- ", cubeVolume)
STDIN Input
Please enter the side 6
Output
Side - 6.0 So, Volume of the cube- 216.0
Here, in the above code, we find the volume of the cube using the following formula ?
var cubeVolume = pow(cubeSide, 3)
Here we use pow() function to calculate the cube of the given cubeSide. So the user input the value of cubeSide is 3, hence the volume of the cube is 64.