IHYPRESS PROGRAMMING
Tutorials and C programs with code and output for beginners
c programming
HOME | ASP | C | CSS | GNUPLOT | HTML | JAVASCRIPT | PERL | PHP | PYTHON | RUBY | SVG
CSelection Structures : Water Bill Calculator II
<03.08>
/* Program for water bill problem with conservation guidelines */ /* * Computes and prints a water bill given an unpaid balance and previous and * current meter readings. Bill includes a demand charge of $35.00, a use * charge of $1.10 per thousand gallons, and a surcharge of $2.00 if there is * an unpaid balance. */ #include <stdio.h> #define DEMAND_CHG 35.00 /* basic water demand charge */ #define PER_1000_CHG 1.10 /* charge per thousand gallons used */ #define LATE_CHG 2.00 /* surcharge assessed on unpaid balance */ #define OVERUSE_CHG_RATE 2.0 /* double use charge as non-conservation penalty */ #define CONSERV_RATE 95 /* percent of last year's use allowed this year */ /* * Displays user instructions */ void instruct_water(void) { printf("This program figures a water bill "); printf("based on the demand charge\n"); printf("($%.2lf) and a $%.2lf per 1000 ", DEMAND_CHG, PER_1000_CHG); printf("gallons use charge.\n\n"); printf("A $%.2lf surcharge is added to ", LATE_CHG); printf("accounts with an unpaid balance.\n"); printf("\nEnter unpaid balance, previous "); printf("and current meter readings\n"); printf("on separate lines after the prompts.\n"); printf("Press or after "); printf("typing each number.\n\n"); } /* * Computes use charge with conservation requirements * Pre: previous, current, and use_last_year are defined. */ double comp_use_charge(int previous, int current, int use_last_year) { int used; /* gallons of water used (in thousands) */ double use_charge; /* charge for actual water use */ used = current - previous; if (used <= CONSERV_RATE / 100.0 * use_last_year) { /* conservation guidelines met */ use_charge = used * PER_1000_CHG; } else { printf("Use charge is at %.2lf times ", OVERUSE_CHG_RATE); printf("normal rate since use of\n"); printf("%d units exceeds %d percent ", used, CONSERV_RATE); printf("of last year's %d-unit use.\n", use_last_year); use_charge = used * OVERUSE_CHG_RATE * PER_1000_CHG; } return (use_charge); } /* * Computes late charge. * Pre : unpaid is defined. */ double comp_late_charge(double unpaid) { double late_charge; /* charge for nonpayment of part of previous balance */ if (unpaid > 0) late_charge = LATE_CHG; /* Assess late charge on unpaid balance. */ else late_charge = 0.0; return (late_charge); } /* * Displays late charge if any and bill. * Pre : late_charge, bill, and unpaid are defined. */ void display_bill(double late_charge, double bill, double unpaid) { if (late_charge > 0.0) { printf("\nBill includes $%.2lf late charge", late_charge); printf(" on unpaid balance of $%.2lf\n", unpaid); } printf("\nTotal due = $%.2lf\n", bill); } int main(void) { int previous; /* input - meter reading from previous quarter in thousands of gallons */ int current; /* input - meter reading from current quarter */ int use_last_year; /* last year usage */ double unpaid; /* input - unpaid balance of previous bill */ double bill; /* output - water bill */ int used; /* thousands of gallons used this quarter */ double use_charge; /* charge for actual water use */ double late_charge; /* charge for nonpayment of part of previous balance */ /* Display user instructions. */ instruct_water(); /* Get data: unpaid balance, previous and current meter readings. */ printf("Enter unpaid balance > $"); scanf("%lf", &unpaid); printf("Enter previous meter reading > "); scanf("%d", &previous); printf("Enter current meter reading > "); scanf("%d", ¤t); printf("Enter last year's reading > "); scanf("%d", &use_last_year); /* Compute use charge. */ use_charge = comp_use_charge(previous, current, use_last_year); /* Determine applicable late charge */ late_charge = comp_late_charge(unpaid); /* Figure bill. */ bill = DEMAND_CHG + use_charge + unpaid + late_charge; /* Print bill. */ display_bill(late_charge, bill, unpaid); return (0); }
Hergestellt in Deutschland / Made in Germany
This program figures a water bill based on the demand charge ($35.00) and a $1.10 per 1000 gallons use charge. A $2.00 surcharge is added to accounts with an unpaid balance. Enter unpaid balance, previous and current meter readings on separate lines after the prompts. Press or after typing each number. Enter unpaid balance > $500 Enter previous meter reading > 1000 Enter current meter reading > 4000 Enter last year's reading > 3000 Use charge is at 2.00 times normal rate since use of 3000 units exceeds 95 percent of last year's 3000-unit use. Bill includes $2.00 late charge on unpaid balance of $500.00 Total due = $7137.00
COPYRIGHT © 2015-2024 IHY PRESS Frankfurt am Main 60329 Deutschland