C Program to Find Volume of a Sphere
This program calculates the volume of a sphere using the formula (4/3) × π × r³. The value of π is defined as a constant in C.
Concept Overview
The volume of a sphere is calculated using simple mathematical formula: Volume = (4/3) × π × r³. In C, we can define π as a constant and perform these calculations easily.
Program
C
#include <stdio.h>
int main() {
float radius, volume;
const float PI = 3.1415926535;
printf("Enter the radius of the sphere: ");
scanf("%f", &radius);
// Volume formula: (4/3) * PI * r^3
volume = (4.0 / 3.0) * PI * radius * radius * radius;
printf("Volume of the sphere with radius %.0f is %f\n", radius, volume);
return 0;
}
Output
Volume of the sphere with radius 5 is 523.583333
Explanation
- `#define pi 3.1415` – Defines the constant value of π used in calculations.
- `int r = 5;` – Declares the radius of the sphere.
- `volume = (4.0/3.0) * pi * r * r * r;` – Calculates the volume using the formula (4/3)πr³.
- `printf()` – Displays the result on the screen.
Note: You can modify the program to take radius input from the user using `scanf()` instead of assigning a fixed value.
Codecrown