/* * Duy Nguyen * * Assignment 2.1 * * This program allows the user to input the number of half dollars, * quarters, dimes, nickels, and pennies. It will then compute the * total number of coins entered and the equivalent number of pennies * entered. * */ #include int main() { int hd, q, d, n, p; // variables declaration int tot_pen, tot_coins; // declaration of output variables cout << "This program asks the user to input 5 things :\n"; cout << " 1) Amount of half dollars\n"; cout << " 2) Amount of quarters\n"; cout << " 3) Amount of dimes\n"; cout << " 4) Amount of nickels\n"; cout << " 5) Amount of pennies\n"; cout << "It will then compute the total number of coins entered \n"; cout << "and the equivalent number of pennies entered.\n"; cout << "Remember that all number entered must be positive integers."; cout << "\nThe program will continue to ask for inputs. Enter -999 "; cout << "\nfor any of the entries to quit the program.\n\n"; while (1) { cout << "Enter half dollars > "; cin >> hd; if (hd == -999) return(0); cout << "Enter quarters > "; cin >> q; if (q == -999) return(0); cout << "Enter dimes > "; cin >> d; if (d == -999) return(0); cout << "Enter nickels > "; cin >> n; if (n == -999) return(0); cout << "Enter pennies > "; cin >> p; if (p == -999) return(0); if ((hd >= 0) && (q >= 0) && (d >= 0) && (n >= 0) && (p >= 0)) { tot_coins = hd + q + d + n + p; tot_pen = 50*hd + 25*q + 10*d + 5*n + p; cout << "You entered :"; cout.width(5); cout << hd << " half dollars \n"; cout.width(18); cout << q << " quarters \n"; cout.width(18); cout << d << " dimes \n"; cout.width(18); cout << n << " nickels \n"; cout.width(18); cout << p << " pennies \n"; cout << "\n"; cout << "You have entered " << tot_coins << " coins"; cout << " for a total of " << tot_pen << " pennies. \n \n"; } else { cout << "You have entered an incorrect input.\n"; cout << "Please re-enter.\n\n"; } // End if } // End while loop } -------------------------------------------------------------------------- /* * Duy Nguyen * * Assignment 2.2 * * This program allows the user to input the number of half dollars, * quarters, dimes, nickels, and pennies. It will then compute the * total number of coins entered and the equivalent number of pennies * entered. * */ #include int main() { int hd, q, d, n, p; // variables declaration int tot_coins; // declaration of output variables double amt; // dollar amount equivalent cout << "This program asks the user to input 5 things :\n"; cout << " 1) Amount of half dollars\n"; cout << " 2) Amount of quarters\n"; cout << " 3) Amount of dimes\n"; cout << " 4) Amount of nickels\n"; cout << " 5) Amount of pennies\n"; cout << "It will then compute the total number of coins entered \n"; cout << "and the equivalent number of pennies entered.\n"; cout << "Remember that all number entered must be positive integers."; cout << "\nThe program will continue to ask for inputs. Enter -999 "; cout << "\nfor any of the entries to quit the program.\n\n"; while (1) { cout << "Enter half dollars > "; cin >> hd; if (hd == -999) return(0); cout << "Enter quarters > "; cin >> q; if (q == -999) return(0); cout << "Enter dimes > "; cin >> d; if (d == -999) return(0); cout << "Enter nickels > "; cin >> n; if (n == -999) return(0); cout << "Enter pennies > "; cin >> p; if (p == -999) return(0); if ((hd >= 0) && (q >= 0) && (d >= 0) && (n >= 0) && (p >= 0)) { tot_coins = hd + q + d + n + p; amt = (50*hd + 25*q + 10*d + 5*n + p)/100.0; cout << "You entered :"; cout.width(5); cout << hd << " half dollars \n"; cout.width(18); cout << q << " quarters \n"; cout.width(18); cout << d << " dimes \n"; cout.width(18); cout << n << " nickels \n"; cout.width(18); cout << p << " pennies \n"; cout << "\n"; cout << "You have entered " << tot_coins << " coins"; cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout << " for a total of $" << amt << ".\n\n"; } else { cout << "You have entered an incorrect input.\n"; cout << "Please re-enter.\n\n"; } // End if } // End while loop } -------------------------------------------------------------------------- /* Duy Nguyen * * Homework 2.3 (sample) * ver 1.0a (1/29/98) * * This program rounds off an integer i to the next largest even * multiple of another integer j. The user is expected to enter * both i and j. * */ #include #define Greater(a, b) (((a)>(b)) ? (a) : (b)) #define Smaller(a, b) (((a)<(b)) ? (a) : (b)) main() { int first_num, sec_num; // the user input integers int larger, smaller; // find smaller and larger int next_multiple; // The next even multiple int test = 0; // Print instructions to the user cout << "This program allows the user to input 2 integers\n"; cout << "It then rounds off the larger number to an even\n"; cout << "multiple of the smaller number. The order of input\n"; cout << "is unimportant. \n\n"; while (!test) { // Get the user's inputs cout << "Enter the two numbers (first_num sec_num) \n"; cout << "Enter 0 for any one of them to quit: "; cin >> first_num >> sec_num; cout << "\n\n"; if ( (first_num == 0) || (sec_num == 0) ) { cout << "\n\nExiting program...\n\n"; test = 1; } else { // Find which of the inputs is smaller and which is larger larger = Greater(first_num, sec_num); smaller = Smaller(first_num, sec_num); if (larger%smaller == 0) { cout << larger << " rounded off to the next largest multiple of " << smaller; cout << " is: " << larger << "\n\n"; } else { /* Round off larger to the next largest even multiple of smaller. */ next_multiple = larger + smaller - (larger % smaller); // Print result cout << larger << " rounded off to the next largest multiple of " << smaller; cout << " is: " << next_multiple << "\n\n"; } } // if-else } // while return 0; // Sucessful finish program } // end main