Question #45495

an electricity board charges the following rates to domestic users:-
for the first 100 units-40p/unit
for the next 200 units-50p/unit
beyond 300 units-60p/unit
write a program to read the names of users and number of units consumed and print out the charges with names.

Expert's answer

#include <iostream>#include <string>int main() {    const int first100 = 40;    const int next200 = 50;    const int beyond300 = 60;    std::string name;    int unitsConsumed, payment = 0;    std::cout << "Please enter your name" << std::endl;    std::cin >> name;    std::cout << "Please enter units consumed" << std::endl;    std::cin >> unitsConsumed;    if ( unitsConsumed <= 100 && unitsConsumed > 0 ) {        payment = first100 * unitsConsumed;    } else if (unitsConsumed > 100 && unitsConsumed <= 300 ) {        payment = next200 * unitsConsumed;    } else if (unitsConsumed > 300 ) {        payment = beyond300 * unitsConsumed;    } else {        std::cout << "Ammount can not be negative. Ending program..." << std::endl;        return 0;    }    std::cout << "Dear " << name << ",\nYou have consumed " << unitsConsumed << " units, which requires " << payment << " credits." << std::endl;    return 0;}

LATEST TUTORIALS
APPROVED BY CLIENTS