Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Jessica Wong
- * Period 4
- * October 3, 2017
- *
- * Directions: When you pay for something with cash, the cashier gives you change
- * in the most efficient way possible (usually). For example, if you get $1.32 change,
- * the cashier will give you 1 dollar, 1 quarter, 1 nickel and 2 pennies (not 132 pennies).
- *
- *Write a program to determine the most efficient denominations of change when one
- * pays $20 for a product worth
- * 1) 17.99
- * 2) 12.53
- * 3) 9.02
- *
- * Your program should make use of $20 as a constant and Print out (for each of the above):
- * If you pay $20 for a product worth $17.99, you will receive as change:
- * 0 ten dollar bills, 0 five dollar bills, 2 one dollar bills, 0 quarters, 0 dimes, 0 nickels, and 1 pennies.
- *
- */
- public class Main {
- public static void main(String[] args) {
- double cost = 17.99;
- double change = 20 - cost;
- int changeInt = (int)(change*100);
- int tens = changeInt/1000;
- changeInt %= 1000;
- int fives = changeInt/500;
- changeInt %= 500;
- int ones = changeInt/100;
- changeInt %= 100;
- int quarters = changeInt/25;
- changeInt %= 25;
- int dimes = changeInt/10;
- changeInt %= 10;
- int nickels = changeInt/5;
- changeInt %= 5;
- int pennies = changeInt;
- System.out.println("If you pay $20 for a product worth" + " $" + cost + " you will receive as change:\n"+ tens + " ten dollar bills, "+fives+
- " five dollar bills, "+ones+ " one dollar bills, "+quarters+" quarters, "+ dimes+" dimes, "+nickels+" nickels, and "+pennies+ " pennies.");
- double cost2 = 12.53;
- double change2 = 20 - cost2;
- int changeInt2 = (int)(change2*100);
- int tens2 = changeInt2/1000;
- changeInt2 %= 1000;
- int fives2 = changeInt2/500;
- changeInt2 %= 500;
- int ones2 = changeInt2/100;
- changeInt2 %= 100;
- int quarters2 = changeInt2/25;
- changeInt2 %= 25;
- int dimes2 = changeInt2/10;
- changeInt2 %= 10;
- int nickels2 = changeInt2/5;
- changeInt2 %= 5;
- int pennies2 = changeInt2;
- System.out.println("If you pay $20 for a product worth" + " $" + cost2 + " you will receive as change:\n"+ tens2 + " ten dollar bills, "+fives2+
- " five dollar bills, "+ones2+ " one dollar bills, "+quarters2+" quarters, "+ dimes2+" dimes, "+nickels2+" nickels, and "+pennies2+ " pennies.");
- double cost3 = 9.02;
- double change3 = 20 - cost3;
- int changeInt3 = (int)(change3*100);
- int tens3 = changeInt3/1000;
- changeInt3 %= 1000;
- int fives3 = changeInt3/500;
- changeInt3 %= 500;
- int ones3 = changeInt3/100;
- changeInt3 %= 100;
- int quarters3 = changeInt3/25;
- changeInt3 %= 25;
- int dimes3 = changeInt3/10;
- changeInt3 %= 10;
- int nickels3 = changeInt3/5;
- changeInt3 %= 5;
- int pennies3 = changeInt3;
- System.out.println("If you pay $20 for a product worth" + " $" + cost3 + " you will receive as change:\n"+ tens3 + " ten dollar bills, "+fives3+
- " five dollar bills, "+ones3+ " one dollar bills, "+quarters3+" quarters, "+ dimes3+" dimes, "+nickels3+" nickels, and "+pennies3+ " pennies.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement