I am asked to calculate the volume and surface area of a flat washer where the outer diameter (d1) and the inner diameter (d2) and height (h) are provided by the user as input. I have solved the program but skeptical if my formulas are correct or not.
For referance
Here is my solved code:
#include <stdio.h>#define pi 3.1416int main(){//declarationfloat D1,D2,r1,r2,volume,surface_area,h; //taking input diameters and hightprintf("Enter outer Diameter of the flat washer :");scanf("%f", &D1); printf("Enter inner Diameter of the flat washer :");scanf("%f", &D2);printf("Enter height of the flat washer: "); scanf("%f", &h); //calculating radiusr1=D1/2; r2=D2/2; //calculating volumevolume=pi*((r1*r1)-(r2*r2))*h; printf("Volume is %.4f\n",volume); //calculating surface areasurface_area=(2*pi*r1*(r1+h))-(2*pi*r2*(r2+h));printf("Surface area is %.4f",surface_area); return 0;}
Best Answer
Your math is correct for volume.
h * (πrouter2 - pirinner2)
with a little factoring produces the formula you have.
Your math is wrong for surface area though. The natural form of the formula of the surface of a washer is
h * 2πrouter + 2(πrouter2 - πrinner2) + h * 2πrinner
which clearly doesn't match the forumula you have. You want:
surface_area=h*2*pi*(r1+r2)+2*pi*(r1*r1-r2*r2);
I'm not really sure but it may help: The formula for the surface could be wrong. Maybe you could try this:
Outer cylinder surface + inner cylinder surface + 2 x cylinder top surface
A = 2*pi*r1*h + 2*pi*r2*h + 2*pi*(r1^2 - r2^2)A = 2*pi * [(r1 + r2) * h + (r1^2 - r2^2)]
Edit: Translating generic formula to c code in respect to Martin's comment. Please note that I'm using M_PI from math.h here.
#include <stdio.h>#include <math.h>int main (void){// ... all the input stuff and volume//calculating surface areasurface_area = 2 * M_PI * ( (r1+r2)*h + r1*r1 - r2*r2 );printf("Surface area is %.4f",surface_area); return 0;}