Write a C program that computes the volume of a sphere.

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>
#define PI 3.142f

int main(void){

	float v;
	int r;
	printf("Enter the radius of the sphere: ");
	scanf("%d", &r);
	v = (4.0f / 3.0f) * PI * r * r * r;
	printf("The volume is %.4f\n", v);
	return 0;

}

When you compile and execute the above program it produces the following result on Linux:

Enter the radius of the sphere: 10
The volume is 4189.3335

Brief Explanation

  • The program starts by requesting the user to enter the radius of the sphere.
  • Using the formula 4/3πr3, the volume of the sphere is computed.
  • Using printf function, the voltage is printed to the stdout.

Add comment


Security code
Refresh