Hello everyone,
I’m working on the optional activity at the end of the ‘C# moneymaker’ problem, where you enter an amount and it returns the minimum number of coins needed to get that amount.
Here is the prompt:
“Use another currency that has different coin amounts. For example, the US uses 1, 5, 10, and 25 cent coins called pennies, nickels, dimes, and quarters, respectively.”
Here is my code:
{
Console.WriteLine(“Welcome to Money Maker!”);
//asks user for amount to convert
Console.WriteLine("What amount would you like to check? Please type a number using number keys, thank you!");
//collects use input and converts to number
string amountentered = Console.ReadLine();
double amountConverted = Convert.ToDouble(amountentered);
amountConverted = Math.Floor(amountConverted);
//tells user what you are about to do
Console.WriteLine($"{amountConverted} is equal to...");
//defines variable of gold and silver coins
int quarterValue = 25;
int dimeValue = 10;
int nickleValue = 5;
//finds maximum number of quarters that fit into amount entered by user
double quarterDivide = Math.Floor(amountConverted / quarterValue);
//find remaining amount
double leftOver = (amountConverted % quarterValue);
//finds maximum number of dimes that fit into amount entered by user
double dimeDivide = Math.Floor(leftOver / dimeValue);
//find remaining amount
leftOver = (dimeDivide % dimeValue);
//finds maximum number of nickels that fit into amount entered by user
double nickleDivide = Math.Floor(leftOver / nickleValue);
//find remaining amount for pennies
leftOver = (nickleDivide % nickleValue);
//returns answer to user
Console.WriteLine($"Quarters: {quarterDivide}.\nDimes: {dimeDivide}.\nNickels:{nickleDivide}.\nPennies:{leftOver}.");
}
The problem is, it returns quarters and dimes, but doesn’t return any nickels or pennies. Even when I enter a total like 249, it returns:
249 is equal to…
Quarters: 9
Dimes: 2
Nickels: 0
Pennies: 0
When the answer I’m looking for is:
Quarters: 9
Dimes: 2
Nickels: 0
Pennies: 4
Any help would be appreciated, thank you!