Logo  

CS256 - Fundamentals of Structured Programming

Displaying exercises/e4/solution/h5.c

#include <stdio.h>

/**
 * Prompt the user to input a wall's height and width. Calculate and output the
 * wall's area (to 2 decimal places) and calculate and output the amount of
 * paint in gallons needed to paint the wall (to 2 decimal places.) Assume a
 * gallon of paint covers 400 square feet.
 * Example input/output:
 * Height: 9
 *  Width: 16
 * Area: 144.00 sq.ft.
 * Paint needed: 0.36 gallons
 */

int main(void)
{
  double width, height;

  printf("Height: ");
  scanf("%lf", &height);
  printf(" Width: ");
  scanf("%lf", &width);
  
  /**
   * We could do all the math in the printf's, so these variables (area, gals)
   * are not that necessary.
   */
  double area = width * height;
  /**
   * In general if you're doing division with constants that could be construed
   * as integers, put a decimal point in there to "promote" the number to a
   * float so as to avoid integer division (unless integer division is what you
   * want of course.)
   */
  double gals = area / 400.0;

  printf("Area: %.2lf sq.ft.\n", area);
  printf("Paint needed: %.2lf gallons\n", gals);

  return 0;
}