C program to calculate the volume of a sphere – In this particular article, we will brief in on the several ways to calculate the volume of a sphere in C programming.
Check out the blog for the suitable examples and sample programs. The compiler has been added as well so that you can check the results yourself.
The methods used to calculate the volume of a sphere in C programming are as follows:
- Using Standard Method
- Using Function
- Using Pointers
- Using Macros
As we all know, a sphere is a 3-dimensional figure made up of infinite points which are equidistant from a single point in the middle. That single point is regarded as the centre of the sphere.
The distance between the surface of the sphere and the centre is regarded as the radius of the sphere. It is an essential component to calculate the volume of a sphere.
A sphere looks like this:
As you can see, the radius of the sphere is denoted by r.
The volume of a sphere can be calculated with this formula:
V = 4/3 * pi * r^3
Using this formula itself, here are the various methods to calculate the volume of a sphere in C programming:
Using Standard Method
1)We have the formula for the volume of a sphere is v=(4*22*r*r)/(7*3);
2)scanf(“%f”,&r) ,scanf function reads the r value, r will be stored into the variable “r”.
3)The r value will substitute into the formula, the calculated value will be store into v, v value will be displayed
using printf(“VOS:%f\n”,v);
1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() { float v,r; printf("enter radius of the sphere: "); scanf("%f",&r); v=(4*22*r*r*r)/(7*3); printf("VOS: %f\n",v); return 0; } |
1 2 |
enter radius of the sphere: 21 VOS: 38808.000000 |
Using Function
1)we are calling the function using volume(r).
2)The called function float volume(float r) will calculate the volume and return the value to v=volume(r).
3)v hold the return value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> float volume(float r) { return (4*22*r*r*r)/(7*3); } int main() { float v,r; printf("enter radius of the sphere: "); scanf("%f",&r); v=volume(r); printf("VOS: %f\n",v); return 0; } |
Using Pointers
1)We are calling the function by passing addresses using volume(&r,&v).
2)volume(float *r,float *v) having pointer variables r,v. This function calculates the volume of the sphere and the value will store into variable v.
3)Using printf statement v value will be displayed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> void volume(float *r,float *v) { *v=(4*22*(*r)*(*r)*(*r))/(3*7); } int main() { float v,r; printf("enter radius:"); scanf("%f",&r); volume(&r,&v); printf("VOS: %f\n",v); return 0; } |
1 2 |
enter radius:10 VOS: 4190.476074 |
Using Macros
1)The symbolic name to the formula (4*22*r*r*r)/(7*3) is area(r)
2)area(r) replaced by the expression given at #define
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> #define area(r) (4*22*r*r*r)/(7*3);// int main() { int r; float a=1; printf("enter radius of the circle: "); scanf("%d",&r); a=area(r); printf("AOC: %f\n",a); return 0; } |
output:
1 2 |
enter radius of the circle: 10 AOC: 4190.000000 |