COMP 104 : Programming

Week 2 : Answer



// extra2-1.cpp
// Print your name in big characters
#include <iostream>
using namespace std;
int main() {
        cout << "H   H  OOO  RRRRR N   N EEEEE RRRRR" << endl
             << "H   H O   O R   R NN  N E     R   R" << endl
             << "HHHHH O   O RRRRR N N N EEE   RRRRR" << endl
             << "H   H O   O R  R  N  NN E     R  R " << endl
             << "H   H  OOO  R   R N   N EEEEE R   R" << endl;
        return 0;
}


// extra2-2.cpp
// Print a 4-digit number by inserting spaces between digits
#include <iostream>
using namespace std;
int main() {
        int number;

        cout << "Enter a 4-digit number: ";
        cin >> number;

        cout << (number / 1000) << "  ";
        number %= 1000;
        cout << (number / 100) << "  ";
        number %= 100;
        cout << (number / 10) << "  ";
        number %= 10;
        cout << number << endl;
        return 0;
}



// extra2-3.cpp
// Print a 4-digit number in reverse order with spaces between digits
#include <iostream>
using namespace std;
int main() {
        int number;

        cout << "Enter a 4-digit number: ";
        cin >> number;

        cout << (number % 10) << "  ";
        number /= 10;
        cout << (number % 10) << "  ";
        number /= 10;
        cout << (number % 10) << "  ";
        number /= 10;
        cout << number << endl;
        return 0;
}


// extra2-4.cpp
// Program to compute a water and sewer bill
#include <iostream>
using namespace std;
int main(){
        double gallons;         //the number of gallons consumed
        double gallon100;       //the number of 100-gallons
        double water;           //water charge,
        double sewer;           //sewer charge,
        double service;         //service charge,
        double total;           //total charge

        //ask user for the number of gallons consumed
        cout << "Enter number of gallons of water consumed: ";
        cin >> gallons;
 
        //calculate values
        gallon100 = gallons/100;
        water = 0.021*gallon100;
        sewer = 0.001*gallon100;
        service = 0.02*(water+sewer);
        total = water+sewer+service;

        //output results
        cout << "Water charge: $" << water << endl;
        cout << "Sewer charge: $" << sewer << endl;
        cout << "Service charge: $" << service << endl;
        cout << "Total charge: $" << total << endl;
        return 0;
}