1 package correctchange; 2 3 import java.util.Scanner; 4 5 public class CorrectChange { 6 7 public static void main(String[] args) { 8 9 Scanner scan = new Scanner(System.in); 10 double change; 11 12 System.out.print("Enter your change (ex: 7.37):\t"); 13 change = scan.nextDouble(); 14 15 int dollar,cent; 16 double getCent; 17 dollar = (int)change % 1000000; 18 getCent = Math.round((change - dollar) * 100); 19 cent = (int)(getCent); 20 21 System.out.println("Change: " + change + ", Dollar: " + dollar + ", getCent: " + cent); 22 23 // dollar calculation 24 25 // number of 100 dollar bill 26 if((dollar / 100) >= 1) 27 { 28 System.out.println(dollar / 100 + " $100"); 29 dollar = dollar - 100 * (dollar / 100); 30 } 31 32 // number of 50 dollar bill 33 if((dollar / 50) >= 1) 34 { 35 System.out.println(dollar / 50 + " $50"); 36 dollar = dollar - 50 * (dollar / 50); 37 } 38 39 // number of 20 dollar bill 40 if((dollar / 20) >= 1) 41 { 42 System.out.println(dollar / 20 + " $20"); 43 dollar = dollar - 20 * (dollar / 20); 44 } 45 46 // number of 10 dollar bill 47 if((dollar / 10) >= 1) 48 { 49 System.out.println(dollar / 10 + " $10"); 50 dollar = dollar - 10 * (dollar / 10); 51 } 52 53 // number of 5 dollar bill 54 if((dollar / 5) >= 1) 55 { 56 System.out.println(dollar / 5 + " $5"); 57 dollar = dollar - 5 * (dollar / 5); 58 } 59 60 // number of 1 dollar bill 61 if((dollar / 1) >= 1) 62 { 63 System.out.println(dollar / 1 + " $1"); 64 } 65 66 // getCents calculation 67 68 // number of quarters 69 if((cent / 25) >= 1) 70 { 71 System.out.println((cent / 25) + " $0.25"); 72 cent = cent - 25 * (cent / 25); 73 } 74 75 // number of dimes 76 if((cent / 10) >= 1) 77 { 78 System.out.println((cent / 10) + " $0.10"); 79 cent = cent - 10 * (cent / 10); 80 } 81 82 // number of nickels 83 if((cent / 5) >= 1) 84 { 85 System.out.println((cent / 5) + " $0.05"); 86 cent = cent - 5 * (cent / 5); 87 } 88 89 // number of pennies 90 if((cent / 1) >= 1) 91 { 92 System.out.println((cent / 1) + " $0.01"); 93 } 94 95 } 96 97 }
Friday, November 22, 2013
Calculating change
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment