Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Wrong output for Volume and Surface Area

$
0
0
The objective of this program is to calculate the volume and surface area of a cylindrical tank with 2 inverted spherical endcaps. Also, the length has to be within a range of 10 and 20 feet and the radius within 3 and 6 feet. Also, the radius can be no more than half the length(the dimensions would not be possible). I have all of the code written, but my problem must be in the equations for volume and surface area. My code:
#include <stdio.h>

#define PI 3.141593f

int main(void)
{
  double length = 0.0;
  double radius = 0.0;
  double check = 0.0;
  double volume = 0.0;
  double surfarea = 0.0;
  
  printf("ACME Truck Company\n");
  printf("This program will calculate the volume and surface area of the X-11 cylindrical tank.");
  
  printf("\nEnter the length of the tank (feet): ");
    scanf("%lf", &length);
  printf("Enter the radius of the tank (feet): ");
    scanf("%lf", &radius);
    
  while(length < 10.0 || length > 20.0) {
    printf("User length is invalid, please enter a value between 10 and 20: ");
      scanf("%lf", &length);
  }
  
  while(radius < 3.0 || radius > 6.0) {
    printf("User radius is invalid, please enter a value between 3 and 6: ");
      scanf("%lf", &radius);
  }
  
  check = (radius * 2);
    
  while(length <= check) {
    printf("User length is invalid, dimensions not possible.");
    check++;
    printf("\nPlease enter a value between %.0lf and 20: ", check);
      scanf("%lf", &length);
      
    if(length < 10.0 || length > 20.0) {
      printf("User length is invalid, please enter a value between 10 and 20: ");
	scanf("%lf", &length);
    }
    check = (radius * 2);
  }
  
  volume = (PI *radius * radius * length) - ((4/3) * PI * radius * radius * radius);
  surfarea = (2 * (double)PI * radius * length) - (4 * (double)PI * radius * radius);
  
  printf("A cylindrical tank of length %.0lf and radius %.0lf with inverted spherical caps has", length , radius);
  printf("\nVolume: %.3lf", volume);
  printf("\nSurface Area: %.3lf", surfarea);
  
}



I'm not sure if my problem has to deal with all of the variables being type double while PI is defined as a float. That's why I tried casting PI to double.

Any help is appreciated!
Thanks.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>