C Program to find the volume of a cone – In this distinct article, we will brief in on all the ways to find the volume of a cone using C programming.
We have added suitable examples and sample programs to the blog post as well. The compiler has been added for an easy understanding of the whole setup.
The methods that have been used to calculate the volume of a cone in C programming are as follows:
- Using Standard Method
- Using Functions
- Using Pointers
- Using Macros
As we all know, a cone is a three-dimensional figure. It is an important figure in use in our daily lives. A cone has a circular base and tapers off into a single point.
From a two-dimensional perspective, it looks like a triangle. A cone looks like this:
As you can see, this is a cone with a radius of 6 units and a height of 8 units.
In order to calculate the volume of a cone, we need to use this formula:
Volume = 1/3 * Pi * r^2 * h
Hence, the volume is this cone is as follows:
Volume = 1/3 * 22/7 * 6^2 * 8 = 301.59 sq. units.
Thus, the ways to calculate the volume of a cone in C programming are as follows:
Using Standard Method
1)We are using the standard formula for calculating the volume of the cone. Here, we need radius and height of the cone.
2)The values of radius and height will store into the variables “radius” and “height”, these values substitute into the formula we will get the volume of a cone, that value will store into variable “volume”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> int main() { float radius,height,volume; printf("enter radius : "); scanf("%f",&radius); printf("enter height : "); scanf("%f",&height); volume=(22*radius*radius*height)/(3*7); printf("VOC: %f\n",volume); return 0; } |
output:
1 2 3 |
enter radius : 7 enter height : 3 VOC: 154.000000 |
Using Functions
1)Here volume(r,h)is calling function,and volume(float r,float h) is called function.
2)We are calling the function using volume(r,h).
3)The called function volume(float r,float h) calculate the volume and returns the value, that value will store into variable “v”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> float volume(float r,float h) { return (22*r*r*h)/(3*7); } int main() { float r,h,v; printf("enter r : "); scanf("%f",&r); printf("enter h: "); scanf("%f",&h); v=volume(r,h); printf("VOC: %f\n",v); return 0; } |
output:
1 2 3 |
enter r : 14 enter h: 3 VOC: 616.000000 |
Using Pointers
1)Here *r,*h,*v are the pointer variables
2)We are calling the function using volume(&r,&h,&v) by passing addresses as arguments.
3)The called function volume(float *r,float *h,float *v) will calculate the volume ,that volume value will store into pointer variable v.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<stdio.h> float volume(float *r,float *h,float *v) { *v= (22*(*r)*(*r)*(*h))/(3*7); } int main() { float r,h,v; printf("enter r : "); scanf("%f",&r); printf("enter h :"); scanf("%f",&h); volume(&r,&h,&v); printf("VOC: %f\n",v); return 0; } |
output:
1 2 3 |
enter r : 70 enter h :3 VOC: 15400.000000 |
Using Macros
1)volume(r,h) is the symbolic name to the formula (22*r*r*h)/(3*7)
2)volume(r,h)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 volume(r,h) (22*r*r*h)/(3*7); int main() { float r,h,v; printf("enter r: "); scanf("%f",&r); printf("enter h:"); scanf("%f",&h); v=volume(r,h); printf("VOC: %f\n",v); return 0; } |
1 2 3 |
enter r: 28 enter h:3 VOC: 2464.000000 |