May 04

Following shows how to use a loop to determine the breakdown of change for a given amount, minimising the number of notes and coins used.

		int[] denominations = { 500, 200, 100, 50, 20, 10, 5, 2, 1 };
		int amount = 687;
		int[] count = new int[denominations.length];
		for (int i=0; i<denominations.length; i++) {
			while (amount>=denominations[i]) {
				count[i]++;
				amount -= denominations[i];
			}
		}
		System.out.println(Arrays.toString(count));

written by objects \\ tags: , , ,


Leave a Reply

You must be logged in to post a comment.