C Program to calculate the Volume of a Cube – In this particular article, we will detail in on the methods to evaluate the volume of a cube in C programming.
The multitude of ways to do so are as follows:
- Using Standard Method
- Using Function
- Using Pointers
- Using Macros
Several examples and sample programs have been added to this blog post as well for you. The compiler has been added for the execution of the programs.
As we all know, the cube is a universally used three-dimensional figure. A cube is a square extrapolated from to give it a height which is similar to the sides as well.
Essentially, all sides of a cube are similar. Opposites sides are parallel and all the angles of a cube are 90 degrees as well. This is how a cube looks like:
As you can see, this is a cube with a side of 3 cm. The volume of a cube can be calculated with this formula:
Volume = Side^3
Hence, the volume of this cube is as follows:
Volume = 3^3 = 27 cm^3.
Thus, the methods used to calculate the volume of a cube in C programming are as follows:
Using Standard Method
1)The value of side of a cube will store into the variable “side”.
2)The value will be substituted into the formula area=side*side*side.
3)The calculated value will store into the variable “area”, and displayed using printfunction.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> int main() { float side,area; printf("enter side of cube: "); scanf("%f",&side); area=side*side*side; printf("VOC: %f\n",area); return 0; } |
1 2 |
enter side of cube: 6 VOC: 216.000000 |
Using Functions
1)The formula for the area of a cube is a=s*s*s.
2)We are calling the function float area(float s) ,using area(s).area(s) is calling function and
area(float s) is called function.
3)Then function float area(float s) return the calculated value, and that value will store into variable “s”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> float area(float s) { return (s*s*s); } int main() { float v,s; printf("enter side of the cube: "); scanf("%f",&s); s=area(s); printf("VOC: %f\n",s); return 0; } |
output:
1 2 |
enter side of the cube: 4 VOC: 64.000000 |
Using Pointers
1)Here *s,*v are pointer variables.
2)We are calling the function using area(&s,&v) by passing addresses of s,v.
3)The function void area(float *s,float *v) calculate the area ,that area value will store into the variable “v”.That v value will be displayed using printf statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include<stdio.h> void area(float *s,float *v) { *v=(*s)*(*s)*(*s); } int main() { float s,v; printf("enter side: "); scanf("%f",&s); area(&s,&v); printf("VOC: %f\n",v); return 0; } |
output:
1 2 |
enter side: 10 VOC: 1000.000000 |
Using Macros
1)#define area(s) represents area(s) is symbolic name to the expression (s*s*s)
2)area(s)replaced with that expression given at #define.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<stdio.h> #define area(s) (s*s*s); int main() { float s,v; printf("enter radius: "); scanf("%f",&s); v=area(s); printf("VOC: %f\n",v); return 0; } |
1 2 |
enter radius: 15 VOC: 3375.000000 |