Posted By

rnavanee on 07/19/13

Statistics

Viewed 526 times
Favorited by 1 user(s)

Determine the minimum number of coins for change

/ Published in: Java

Determine the minimum number of coins for change
Given any number between 1 and 99, determine how to give change with the minimum number of coins. You can assume that the coins are 1c, 2c, 5c, 10c, 20c and 50c.

Copy this code and paste it in your HTML
`import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; public class ChangeCalculator {     /**     * @param args     */    public static void main(String[] args) {        /*Scanner scn = new Scanner(System.in);        Integer amount = scn.nextInt();*/        Integer amount = 105;        Map<String, Integer> changeMap = getChange(amount);        System.out.println(changeMap);     }     private static Map<String, Integer> getChange(Integer amount) {        Map<String, Integer> changeMap = new HashMap<String, Integer>();        List<Integer> changeList = new ArrayList<Integer>();        changeList.add(50);        changeList.add(20);        changeList.add(10);        changeList.add(5);        changeList.add(2);        changeList.add(1);        Integer remAmt = amount;        for (Integer change : changeList) {            Integer changeCount = (remAmt / change);            changeMap.put(change+"c", changeCount);            remAmt = remAmt - (changeCount*change);        }        return changeMap;    }     //private static }`