Advertisement
pro-themes

Making Change

Jun 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. /**
  2.  * Jessica Wong
  3.  * Period 4
  4.  * October 3, 2017
  5.  *
  6.  * Directions: When you pay for something with cash, the cashier gives you change
  7.  * in the most efficient way possible (usually). For example, if you get $1.32 change,
  8.  * the cashier will give you 1 dollar, 1 quarter, 1 nickel and 2 pennies (not 132 pennies).
  9.  *
  10.  *Write a program to determine the most efficient denominations of change when one
  11.  * pays $20 for a product worth
  12.  *  1) 17.99
  13.  *  2) 12.53
  14.  *  3) 9.02
  15.  *
  16.  * Your program should make use of $20 as a constant and Print out (for each of the above):
  17.  *  If you pay $20 for a product worth $17.99, you will receive as change:
  18.  *  0 ten dollar bills, 0 five dollar bills, 2 one dollar bills, 0 quarters, 0 dimes, 0 nickels, and 1 pennies.
  19.  *
  20.  */
  21.  
  22. public class Main {
  23.  
  24.     public static void main(String[] args) {
  25.        
  26.         double cost = 17.99;
  27.         double change = 20 - cost;
  28.         int changeInt = (int)(change*100);
  29.         int tens = changeInt/1000;
  30.         changeInt %= 1000;
  31.         int fives = changeInt/500;
  32.         changeInt %= 500;
  33.         int ones = changeInt/100;
  34.         changeInt %= 100;
  35.         int quarters = changeInt/25;
  36.         changeInt %= 25;
  37.         int dimes = changeInt/10;
  38.         changeInt %= 10;
  39.         int nickels = changeInt/5;
  40.         changeInt %= 5;
  41.         int pennies = changeInt;
  42.        
  43.         System.out.println("If you pay $20 for a product worth" + " $" + cost + " you will receive as change:\n"+ tens + " ten dollar bills, "+fives+
  44.         " five dollar bills, "+ones+ " one dollar bills, "+quarters+" quarters, "+ dimes+" dimes, "+nickels+" nickels, and "+pennies+ " pennies.");
  45.        
  46.         double cost2 = 12.53;
  47.         double change2 = 20 - cost2;
  48.         int changeInt2 = (int)(change2*100);
  49.         int tens2 = changeInt2/1000;
  50.         changeInt2 %= 1000;
  51.         int fives2 = changeInt2/500;
  52.         changeInt2 %= 500;
  53.         int ones2 = changeInt2/100;
  54.         changeInt2 %= 100;
  55.         int quarters2 = changeInt2/25;
  56.         changeInt2 %= 25;
  57.         int dimes2 = changeInt2/10;
  58.         changeInt2 %= 10;
  59.         int nickels2 = changeInt2/5;
  60.         changeInt2 %= 5;
  61.         int pennies2 = changeInt2;
  62.        
  63.         System.out.println("If you pay $20 for a product worth" + " $" + cost2 + " you will receive as change:\n"+ tens2 + " ten dollar bills, "+fives2+
  64.         " five dollar bills, "+ones2+ " one dollar bills, "+quarters2+" quarters, "+ dimes2+" dimes, "+nickels2+" nickels, and "+pennies2+ " pennies.");
  65.        
  66.         double cost3 = 9.02;
  67.         double change3 = 20 - cost3;
  68.         int changeInt3 = (int)(change3*100);
  69.         int tens3 = changeInt3/1000;
  70.         changeInt3 %= 1000;
  71.         int fives3 = changeInt3/500;
  72.         changeInt3 %= 500;
  73.         int ones3 = changeInt3/100;
  74.         changeInt3 %= 100;
  75.         int quarters3 = changeInt3/25;
  76.         changeInt3 %= 25;
  77.         int dimes3 = changeInt3/10;
  78.         changeInt3 %= 10;
  79.         int nickels3 = changeInt3/5;
  80.         changeInt3 %= 5;
  81.         int pennies3 = changeInt3;
  82.        
  83.         System.out.println("If you pay $20 for a product worth" + " $" + cost3 + " you will receive as change:\n"+ tens3 + " ten dollar bills, "+fives3+
  84.         " five dollar bills, "+ones3+ " one dollar bills, "+quarters3+" quarters, "+ dimes3+" dimes, "+nickels3+" nickels, and "+pennies3+ " pennies.");
  85.        
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement