0

I need to make a Windows Form Application that will tell you how much of each type of coin you will need for the amount entered.To start I have a form with 5 text boxes and a button. 1st text box is for the user to enter the amount they have. The rest are read only so they will only display how many of each coin you will need to get that amount the user enters. Button is there so they can push it and then calculate the number of each coin they need will be displayed.

Example: - If user enters .15 then after pushing button in text box for dime will be 1 and text box for nikel will be 1.

I'm having trouble because I'm not sure what I can do next after making an array for each of the coins. I know the next step is for the form to be able to take the entered amount and see which coins will get to entered amount but I'm not sure how to start it. Here my array thats inside the click event for my calculate button. Does anyone have a suggestion? Thank you (* change is the name of array as in coins are change, don't want to change the array)

     int[] change= new int[3];
        change[0] = 1;
        change[1] = 5;
        change[2] = 10;
        change[3] = 25;
6
  • 2
    Something like this? Start with the highest value, see how many you can fit in. Once you've done that, work down through the values until you get to the result. So for example 98 can have 3x25, which leaves 23. 23 can have 2x10, which leaves 3. ? Commented Feb 4, 2020 at 22:20
  • Incidentally I was confused as at first I thought you wanted to 'change the array', when in fact you have an 'array of change' :) Commented Feb 4, 2020 at 22:22
  • That's the part I'm having trouble with I'm not sure what to use in code to check how many times a number goes into the amount entered. I was thinking about an if statement but I'm not sure what I would put inside. Commented Feb 4, 2020 at 22:23
  • 1
    This might help you get started: int quarters = (target - (target % quarter)) / quarter; Commented Feb 4, 2020 at 22:32
  • 1
    These are the docs on the Remainder operator - % as they say, it returns "the remainder after dividing its left-hand operand by its right-hand operand" - you may still have some edge cases to handle, but this should be enough for you to write the code. Commented Feb 4, 2020 at 22:35

1 Answer 1

1

You can use the Math.DivRem method to do that.

Example:

var amount = new Random().Next(1, 101);

Console.WriteLine($"{amount} Change:");

var Quarters = Math.DivRem(amount, 25, out amount);
var Dimes = Math.DivRem(amount, 10, out amount); 
var Nickels = Math.DivRem(amount, 5, out int Pennies);

Console.WriteLine($"Quarters: {Quarters}, Dimes: {Dimes}, Nickels: {Nickels}, Pennies: {Pennies}");

Here's some outputs:

52 Change:
Quarters: 2, Dimes: 0, Nickels: 0, Pennies: 2

34 Change:
Quarters: 1, Dimes: 0, Nickels: 1, Pennies: 4

11 Change:
Quarters: 0, Dimes: 1, Nickels: 0, Pennies: 1

99 Change:
Quarters: 3, Dimes: 2, Nickels: 0, Pennies: 4

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.