(* Author: James Tam Version: May 12 2008 (The example problem comes from "Pascal: An introduction to the Art and Science of Programming" by Walter J. Savitch" but the solution was implemented by James Tam. A program that makes change. Features: * Given a value between 1 and 99 cents it will calculate the number of quarters, dimes and pennies to give back as change. * It will first try to determine the amount of change in larger denominations before calculating on change for smaller demonations e.g., for 37 cents the change to be returned will come out as: 1 quarter, 1 dime and 2 pennies. Limits: * Assumes that the cashier can calculate change for amounts greater than $1.00 * Doesn't calculate the number of nickels. # Doesn't check that the amount owed is actually within the range of 1 - 99 cents. *) program change (input, output); begin (* Declaring and initializing variables. *) var amount : integer; var quarters : integer; var dimes : integer; var pennies : integer; var amountLeft : integer; amount := 0; quarters := 0; dimes := 0; pennies := 0; amountLeft := 0; (* Prompt for the amount owed. *) write ('Enter the amount of change from 1 to 99 cents: '); readln (amount); (* Algorithm: Compute number of quarters to return. The number of quarters will simply be the integer result of dividing the original amount owed by 25 (the numbe of quarters that the original amount evenly divides into). The remaining change is the modolo of the original amount owed divided by 25. *) quarters := amount DIV 25; amountLeft := amount MOD 25; (* Algorithm: Compute number of dimes to return. The number of dimes will simply be the integer result of dividing the new amount owed from the previous step by 10 (the number of dimes that the new amount evenly divides into). The remaining change is the modolo of the new amount owed (previous step) divided by 10. *) dimes := amountLeft DIV 10; amountLeft := amountLeft MOD 10; (* Algorithm: Given that the number of quarters and dimes to give back as change has been calculated then the number of pennies is simply the remaining change owed. *) pennies := amountLeft; (* Display the results. *) writeln ('Original amount: ', amount, ' pennies'); writeln ('No quarters: ', quarters); writeln ('No dimes: ', dimes); writeln ('No pennies: ', pennies); end.