user12822426
user12822426

Reputation:

Windows Form Application C# - Change Array

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;

Upvotes: 0

Views: 194

Answers (1)

user10216583
user10216583

Reputation:

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

Upvotes: 1

Related Questions