-3

"Given an amount of change less than one dollar, find the coins required to make up this amount. Your program should find the minimum number of coins. For example, if the change was $0.56, you would need 2 quarters, 1 nickel and 1 penny for a total of 4 coins. Hint: Use integer division and remainder."

I have to write code for this on java for a school assignment. I am not allowed to use if statements, how do I do this?

NickL
  • 4,258
  • 2
  • 21
  • 38
  • If all you're concerned about is not being able to use an if block, `if (cond) { block }` is equivalent to `for (;cond;) { block; break; }` or `while (cond) { block; break; }`. – Andy Turner Oct 07 '17 at 21:30
  • [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/3788176) – Andy Turner Oct 07 '17 at 21:31
  • Your instructor is incorrect. 1 [half dollar](https://en.wikipedia.org/wiki/Half_dollar_(United_States_coin)), 1 nickel and 1 penny use three coins. Also acceptable should be 1 half dollar, and 2 [three cent nickels](https://en.wikipedia.org/wiki/Three-cent_nickel). 3 coins is less than 4. – Elliott Frisch Oct 07 '17 at 21:32
  • 2
    Kandiah, can you make a list of which coins you are allowed to use ? Most people here are not familiar with American money, and it seems like you have restrictions on which you can use, as your example doesn't show the minimum number of coins required to get 0.56$. – Paul Lemarchand Oct 07 '17 at 21:36
  • 1
    Stack Overflow is a question-and-answer site, not a homework-writing service. We are not going to do your homework for you. [An open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) – Joe C Oct 07 '17 at 22:01
  • I'm voting to close this question as off-topic because [**"Questions asking for *homework help* must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it."**](https://stackoverflow.com/help/on-topic). – Pang Oct 20 '17 at 01:30

1 Answers1

-2

Like the hint says, use integer division and remainder. Java has the % (modulus) operator to calculate the remainder.

Assuming the change is an int defined in cents (so 99 cents, not 0.99 dollar), you could do something like this:

int coins = change/25 + change%25/10 + ... ;
NickL
  • 4,258
  • 2
  • 21
  • 38
  • 1
    Please don't encourage these "do my homework" questions by answering them. Thank you. – Joe C Oct 07 '17 at 22:02
  • I have not provided the complete answer to the assignment, OP still has to understand modulus and integer division to finish it. – NickL Oct 10 '17 at 14:09