{"id":175,"date":"2011-03-28T16:51:37","date_gmt":"2011-03-28T05:51:37","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=175"},"modified":"2011-07-05T17:23:56","modified_gmt":"2011-07-05T07:23:56","slug":"money-change-breakdown","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/money-change-breakdown\/","title":{"rendered":"Money Change Breakdown"},"content":{"rendered":"

What is the smallest number of coins (or notes) that are required to give a specified amount of change? This is the next problem we will address.<\/p>\n

To solve this problem we use basically the same process a shop keeper would when giving change to customers. That is we look for the largest denomination coin\/note that is less than the amount of change still required to be paid. We repeat this process until all the change is paid. <\/p>\n

\r\n\r\npackage com.learnjava.math;\r\n\r\nimport java.util.Scanner;\r\n\r\npublic class MoneyBreakdown {\r\n\r\n\t\/**\r\n\t * @param args\r\n\t *\/\r\n\tpublic static void main(String[] args) {\r\n\t\t\r\n\t\tfinal int[] denominations = \r\n\t\t\t{ 500, 200, 100, 50, 20, 10, 5, 2, 1 };\r\n\t\t\r\n\t\tScanner scanner = new Scanner(System.in);\r\n\t\t\r\n\t\t\/\/ Get the amount to breakdown from user\r\n\t\t\r\n\t\tSystem.out.print("Enter amount (in cents): ");\r\n\t\tint amount = scanner.nextInt();\r\n\t\t\r\n\t\t\/\/ Determine how many of each denomination are required\r\n\t\t\r\n\t\tint[] count = breakdown(denominations, amount);\r\n\t\t\r\n\t\t\/\/ Output the result\r\n\t\t\r\n\t\tfor (int i=0; i<denominations.length; i++) {\r\n\t\t\tif (count[i]>0) {\r\n\t\t\t\tSystem.out.println(count[i]+" x "+denominations[i]);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tprivate static int[] breakdown(int[] denominations, int amount) {\r\n\t\tint[] count = new int[denominations.length];\r\n\t\t\r\n\t\t\/\/ Loop through each denomination (starting at largest)\r\n\t\t\r\n\t\tfor (int i=0; i<denominations.length; i++) {\r\n\t\t\t\r\n\t\t\t\/\/ Use one of that denomination until we need something smaller\r\n\t\t\t\r\n\t\t\twhile (amount>=denominations[i]) {\r\n\t\t\t\tcount[i]++;\r\n\t\t\t\tamount -= denominations[i];\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn count;\r\n\t}\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

What is the smallest number of coins (or notes) that are required to give a specified amount of change? This is the next problem we will address. To solve this problem we use basically the same process a shop keeper would when giving change to customers. That is we look for the largest denomination coin\/note…<\/p>\n","protected":false},"author":3,"featured_media":177,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4],"tags":[43,6,42],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/learn-java-by-example.com\/wp-content\/uploads\/2011\/07\/coins.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-2P","jetpack-related-posts":[{"id":56,"url":"https:\/\/learn-java-by-example.com\/java\/monthly-payment-calculator\/","url_meta":{"origin":175,"position":0},"title":"Monthly Payment Calculator","date":"June 2, 2010","format":false,"excerpt":"This is the simplest form of one of the classic problems given to first year Java students. It aims to get you comfortable with the structure of a simple Java application and how to get input from the user. User input in this example is taken from the console (standard\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":182,"url":"https:\/\/learn-java-by-example.com\/java\/high-guessing-game\/","url_meta":{"origin":175,"position":1},"title":"High Low Guessing Game","date":"May 17, 2011","format":false,"excerpt":"The assignment here is to write a simple game where the user tries to guess a randomly selected number. After each guess the application tells the user if the guess is too high or too low. This is repeated until the user finally guesses the number. First thing we need\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":175,"position":2},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":160,"url":"https:\/\/learn-java-by-example.com\/java\/pascals-triangle\/","url_meta":{"origin":175,"position":3},"title":"Pascals Triangle","date":"February 13, 2011","format":false,"excerpt":"Write a Java application that prints the first 10 lines of Pascals Triangle. Each row of a Pascals Triangle can be calculated from the previous row so the core of the solution is a method that calculates a row based on the previous row which is passed as input. Once\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":133,"url":"https:\/\/learn-java-by-example.com\/java\/fibonnaci-number\/","url_meta":{"origin":175,"position":4},"title":"Fibonnaci Number","date":"December 3, 2010","format":false,"excerpt":"Generating Fibonacci numbers is an assignment often given to Java students when they are being introduced to recursion. Recursion is programming technique that involves a method calling itself to solve a problem. When implementing a recursive solution we look for two things: A base case that returns a value without\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":222,"url":"https:\/\/learn-java-by-example.com\/java\/list-array\/","url_meta":{"origin":175,"position":5},"title":"Using a List instead of an array","date":"March 8, 2012","format":false,"excerpt":"Lists and arrays can both be used to store ordered collections of data. Both have their strengths and weakness which we shall discuss in a later post. Previously we showed you how to generate Pascals Triangle and in that Java example we used arrays to represent each row of the\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/175"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":5,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":329,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/175\/revisions\/329"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media\/177"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}