5.9k views
2 votes
Write a program to calculate surface area and volume of a sphere (ball). The program will ask user to input radius (meter(s)) of a sphere and return its area and volume (output should round to three decimal places). Use macro #define for value of π (suppose π = 3.1415927). Learn to use pow function for evaluating square and cubic in math.h of C programming library (Google ""pow c programming""). You can use scanf function and double type for radius input. Name your program file Hw2 Q1 Code.c.

User Occhiso
by
8.2k points

1 Answer

5 votes

Answer:

The following program is to calculate the surface area and volume of a sphere(ball) using macro in c language .

#include <math.h> //header file for math functions

#define pi 3.1415 //using macro

#include <stdio.h> //header file for printf() or scanf()

int main() //main function

{

double radius; //radius is a double type variable

float surface_area, vol; //surface_area andvolume is a float type variable

printf("Enter the radius of the sphere : \\"); //printf() is use to print an output

scanf("%lf", &radius); //scanf() is use to take input from the user

surface_area = 4 * pi * pow(radius,2);

vol = (4.0/3) * pi * pow(radius,3);

printf("The Surface area of the sphere is: %.3f", surface_area); // display surface area

printf("\\ Volume of the sphere is : %.3f", vol); // display volume

return 0;

}

Output:

Enter radius of the sphere : 4.4

Surface area of sphere is: 243.278

Volume of sphere is : 356.807

Step-by-step explanation:

Here in this program we include the header file math.h and include the macro pi. after that taking the user input in radius and calculating the surface area and volume of sphere and finally display the result.

User Bert Bruynooghe
by
8.6k points