# C# Moneymaker Project: Optional activity

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!

The problem you have is that, when calculating dimeDivide and nickleDivide, you are trying to find the remainder after dividing the calculated number of either dimes or nickels with dime/nickel value.

It helps if you take it sequentially.

1. 249 is input.
2. The quarter value 25 divides into 249 9 times exactly with 24 remaining. This gives us a result of 9 quarters and 24 left over.
3. The dime value 10 divides into the remainder, 24 twice with 4 remaining. This gives us a result of 2 dimes with 4 remaining. However when you calculate the remainder you use the dime result instead of what is left over (4) meaning that you calculate the left over to be 2 instead of 4.
4. The remainder, incorrectly calculated above, 2 divides 0 times into the nickel value of 5 with a remainder of two. However you use the nickel result of 0. 0 % anything = 0.

What you want to do is instead use the working value of left over to mod against the nickel/dime value.

Refactoring only slightly you would have:

``````//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 userInput = Console.ReadLine();
double workingAmount = Math.Floor(Convert.ToDouble(userInput));

//tells user what you are about to do

Console.WriteLine(\$"{workingAmount} 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(workingAmount / quarterValue);

//find remaining amount

workingAmount = (workingAmount % quarterValue);

//finds maximum number of dimes that fit into amount entered by user

double dimeDivide = Math.Floor(workingAmount / dimeValue);

//find remaining amount

workingAmount = (workingAmount % dimeValue);

//finds maximum number of nickels that fit into amount entered by user

double nickleDivide = Math.Floor(workingAmount / nickleValue);

//find remaining amount for pennies

workingAmount = (workingAmount % nickleValue);

//returns answer to user

Console.WriteLine(\$"Quarters: {quarterDivide}.\nDimes: {dimeDivide}.\nNickels:{nickleDivide}.\nPennies:{workingAmount}.");
``````

This gives the right output.

One thing I would recommend is that, if you want to try and convert a user inputted value to a double, you wrap it in a try catch block. At the moment if the user inputs a value such as “Ten” the program will throw an error. You can learn more about that here: Exception-handling statements - throw and try, catch, finally - C# | Microsoft Learn but it might be beyond the scope of your journey at this time - you can always take a look later when you have learned more about C#!

Sometimes issues like this can be pretty tricky to spot. I was very puzzled to begin with. It wasn’t until I copied your code into visual studio and debugged it I could see what was happening. Codecademy doesn’t have debugging in it but you can always try running the code in a local console project with visual studio to help spot bugs like this.

Hope that helps!

1 Like