Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a homework program but I am stuck on it. I did some code but still I don\

ID: 653737 • Letter: I

Question

I have a homework program but I am stuck on it. I did some code but still I don't know how to do it so the output will display. I attached the problem so you can see what is the real problem. Please help me if anyone knows about this or can give me an idea how to develop the program. Thank you.

Here is what I came up with:

import java.util.Scanner;
import java.lang.*;
import java.io.*;
public class CoinConverter {
//constructor
public static void main(String[] args){
int amount;
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount");
amount = in.nextInt();
if(amount%5==0)
change(amount, 0, 0, 0);
else
System.out.println("Cannot proceed");
}
//recursive method to make change
public static void change(int amount, int quarters, int dimes, int nickels)
{
//if amount to be changed is greater than 25, subtract a quarter and call again
if (25<=amount&&dimes==0&&nickels==0)
{
change(amount-25, quarters+1, dimes, nickels);
}

//if amount is greater than 10, subtract a dime and call again
if (10<=amount&&nickels==0)
{
change(amount-10, quarters, dimes+1, nickels);
}
//if amount is greater than 5, subtract a nickel and call again
if (5<=amount)
{
change(amount-5, quarters, dimes, nickels+1);
}

I think I have to make a loop or switch statement to display the outputs for the program.

Explanation / Answer

private static int findMinCoins(final int[]coins, final int sum){
    int[] calculationsCache = new int[sum+1];
    for(int i = 0; i <= sum; i++){
        calculationsCache[i] = Integer.MAX_VALUE;
    }
    calculationsCache[0]=0;/*sum 0 , can be made with 0 coins*/
    for(int i = 1; i <= sum; i++){
        for(int j = 0; j < coins.length; j++){
            if(i >= coins[j] && i - coins[j] >= 0 && calculationsCache[i-coins[j]]+1 < calculationsCache[i]){
                calculationsCache[i] = calculationsCache[i-coins[j]]+1;
            }
        }
    }
    return calculationsCache[sum];
}