binibiningtinamoran

MoneyConverter

Apr 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MoneyConverter {
  4.  
  5. public static void main(String []args) {
  6.  
  7. final int TENS = 10;
  8. final int FIVES = 5;
  9.  
  10. final int QUARTERS = 25;
  11. final int DIMES = 10;
  12. final int NICKELS = 5;
  13.  
  14. Scanner scan = new Scanner(System.in);
  15.  
  16. System.out.print("Enter a monetary value: ");
  17. double amount = scan.nextDouble();
  18.  
  19. int numTenBills = (int) Math.floor(amount / TENS);
  20. int numFiveBills = (int) ((amount - (TENS * numTenBills)) / FIVES);
  21. int numOneBills = (int) ((amount - ((numTenBills*TENS) + (numFiveBills*FIVES))));
  22.  
  23. double totalCentsCounts = Math.round((amount % 1) * 100);
  24. int numQuarters = (int) totalCentsCounts / QUARTERS;
  25. int numDimes = (int) (totalCentsCounts - (QUARTERS * numQuarters)) / DIMES;
  26. int numNickels =
  27. (int) (totalCentsCounts - ((numQuarters*QUARTERS) + (numDimes*DIMES))) / NICKELS;
  28. int numPennies =
  29. ((int) totalCentsCounts - ((QUARTERS * numQuarters) + (numDimes*DIMES) + (numNickels*NICKELS)));
  30.  
  31. System.out.printf("\n%,d ten dollar %s", numTenBills, (numTenBills > 1 ? "bills" : "bill"));
  32. System.out.printf("\n%,d five dollar %s", numFiveBills, (numFiveBills > 1 ? "bills" :
  33. "bill"));
  34. System.out.printf("\n%,d one dollar %s", numOneBills, (numOneBills > 1 ? "bills" : "bill"));
  35.  
  36. System.out.printf("\n%,d %s", numQuarters, (numQuarters > 1 ? "quarters" : "quarter"));
  37. System.out.printf("\n%,d %s", numDimes, (numDimes > 1 ? "dimes" : "dime"));
  38. System.out.printf("\n%,d %s", numNickels, (numNickels > 1 ? "nickels" : "nickel"));
  39. System.out.printf("\n%,d %s", numPennies, (numPennies > 1 ? "pennies" : "penny"));
  40.  
  41. }
  42. }
Add Comment
Please, Sign In to add comment