In: Computer Science
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
Code :-
#include <stdio.h>
int main()
{
float pi=3.14159 ;
float r , area , volume ;
printf(" Enter radius ");
scanf("%f",&r); //storing in float variable
area = pi * r * r ; //calculating area
volume= (4 *pi*r*r*r)/3; //calculating volume
printf(" area of circle = %f \n",area);
printf(" volume of sphere = %f ",volume);
return 0;
}
-----------------------------------------------------------------------------------
Note :- In programming , (4/3) returns 1 . This is because 4 is an integer and 3 is an integer . Therefore , output wil also be an integer irrespective of operator.
In order to get correct value i.e. 1.33 we multiply 4 with a float variable (which is done in the above program).Now , operations performed on float variable and integer variable will always results in float value .
-----------------------------------------------------------------------------------
Code with output screenshot:-
-------------------------------------------------------------------------------------